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

皈依舞
TA貢獻(xiàn)1851條經(jīng)驗(yàn) 獲得超3個(gè)贊
#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...

湖上湖
TA貢獻(xiàn)2003條經(jīng)驗(yàn) 獲得超2個(gè)贊
+new
+alloc/-init
NSObject
+new
+new
+new
.
- 3 回答
- 0 關(guān)注
- 440 瀏覽
添加回答
舉報(bào)
0/150
提交
取消