我只是想知道一个对象何时被alloc分配,并且分配了一块内存,为什么init不使用那段内存并更改对象的地址?
NSDate *t = nil;
NSLog(@"t = %p",t); // t = 0x0
t = [NSDate alloc];
NSLog(@"t = %p",t); // t = 0x100107af0
t = [t init];
NSLog(@"t = %p",t); // t = 0x1001035a0
Run Code Online (Sandbox Code Playgroud) static以下代码究竟做了什么,何时应该或更好地使用Static:
// Returns an array of 30 odd numbers
- (NSArray *)odds
{
static NSMutableArray *odds;
odds = [[NSMutableArray alloc] init];
int i = 1;
while ([odds count] < 30) {
[odds addObject:[NSNumber numberWithInt:i]];
i += 2;
}
return odds;
}
Run Code Online (Sandbox Code Playgroud)