使用alloc init而不是新的学习目标-C和读取示例代码时,我注意到对象通常是使用这种方法创建的:SomeObject *myObject = [[SomeObject alloc] init];而不是:SomeObject *myObject = [SomeObject new];正如我所读到的,这是否有其原因?
3 回答
![?](http://img1.sycdn.imooc.com/5333a1920001d36402200220-100-100.jpg)
绝地无双
TA贡献1946条经验 获得超4个赞
#import "InitAllocNewTest.h"@implementation InitAllocNewTest+(id)alloc{ NSLog(@"Allocating..."); return [super alloc];}-(id)init{ NSLog(@"Initializing..."); return [super init];}@end
[[InitAllocNewTest alloc] init];
[InitAllocNewTest new];
2013-03-06 16:45:44.125 XMLTest[18370:207] Allocating... 2013-03-06 16:45:44.128 XMLTest[18370:207] Initializing...
![?](http://img1.sycdn.imooc.com/54584f3100019e9702200220-100-100.jpg)
函数式编程
TA贡献1807条经验 获得超9个赞
+new
+alloc/-init
NSObject
+new
+new
+new
.
- 3 回答
- 0 关注
- 349 浏览
添加回答
举报
0/150
提交
取消