使用alloc init而不是新的學(xué)習(xí)目標(biāo)-C和讀取示例代碼時,我注意到對象通常是使用這種方法創(chuàng)建的:SomeObject *myObject = [[SomeObject alloc] init];而不是:SomeObject *myObject = [SomeObject new];正如我所讀到的,這是否有其原因?
3 回答

湖上湖
TA貢獻(xiàn)2003條經(jīng)驗 獲得超2個贊
#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...

一只名叫tom的貓
TA貢獻(xiàn)1906條經(jīng)驗 獲得超3個贊
+new
+alloc/-init
NSObject
+new
+new
+new
.
- 3 回答
- 0 關(guān)注
- 494 瀏覽
添加回答
舉報
0/150
提交
取消