iOS new 和 alloc的区别

一般如果只是 alloc init 那么 用new 是一样的 

SomeObject*myObject =[[SomeObject alloc] init];
SomeObject*myObject =[SomeObjectnew];

即上述两种情况是一样的!
#import "InitAllocNewTest.h"

@implementation InitAllocNewTest

+(id)alloc{
    NSLog(@"Allocating...");
    return [super alloc];
}
-(id)init{
    NSLog(@"Initializing...");
    return [super init];
}
@end
In main function both statements:

[[InitAllocNewTest alloc] init];
and

[InitAllocNewTest new];
result in the same output:

2013-03-06 16:45:44.125 XMLTest[18370:207] Allocating...
2013-03-06 16:45:44.128 XMLTest[18370:207] Initializing...

Some selected ones are:

  • new doesn't support custom initializers (like initWithString)
  • alloc-init is more explicit than new
posted @ 2013-09-17 10:17  cocoajin  阅读(524)  评论(0)    收藏  举报