bkb*_*abs 0 iphone optimization memory-management casting objective-c
在内存和计算时间方面,最好将int转换为:
int thisLetterInt = (int)[(NSNumber *)[levelSavedSolutionArrayFIX objectAtIndex:i] intValue];
Run Code Online (Sandbox Code Playgroud)
要么
int thisLetterInt = [[levelSavedSolutionArrayFIX objectAtIndex:i] intValue];
Run Code Online (Sandbox Code Playgroud)
还是完全相同?
另外,在任何一种情况下,我都不负责释放吗?
转换是不必要的,您只是在程序编译期间浪费硬盘空间和CPU周期.
即使启用了ARC,编译器也会知道对于类型id(-objectAtIndex:返回的是什么),最可能的方法签名-intValue是i@:或,方法返回一个整数并取两个参数(self和_cmd).
这会产生影响的唯一地方是你使用点语法,例如.
int thisLetterInt = [levelSavedSolutionArrayFIX objectAtIndex:i].intValue; // compile error
Run Code Online (Sandbox Code Playgroud)
因为类型id没有向编译器显示任何属性,所以必须先将其强制转换.
int thisLetterInt = ((NSNumber *)[levelSavedSolutionArrayFIX objectAtIndex:i]).intValue; // compiles fine
Run Code Online (Sandbox Code Playgroud)
就释放而言,不会在堆栈上分配原始类型,而不是堆,这意味着在退出当前作用域后它们将被自动清除.
| 归档时间: |
|
| 查看次数: |
1010 次 |
| 最近记录: |