有人能告诉我为什么 localComplete 块和 self.block 的内存地址是一样的吗?self.complete 的属性设置为复制,只是为了确保在将其分配给 self.complete 时我也在 localComplete 上调用复制。
- (void) test {
CompletionBlock localComplete = ^{
};
NSLog(@"localComplete - %p", localComplete);
self.block = [localComplete copy];
NSLog(@"self.complete - %p", self.block);
self.block();
}
这是输出:
2013-10-05 08:39:18.549 TestApp[90703:a0b] localComplete - 0x60b8
2013-10-05 08:39:18.550 TestApp[90703:a0b] self.complete - 0x60b8
作为另一个示例,我创建字符串:
// creating string
self.carType = [[NSString alloc] initWithFormat: @"Good%@", @"year"];
NSLog(@"self.carType - %p", self.carType);
// same memory address???
NSString *carInitString = [[NSString alloc] initWithString: self.carType];
NSLog(@"carInitString - %p", carInitString);
// same memory address???
NSString *carCopy = [self.carType copy];
NSLog(@"carCopy - %p", carCopy);
// different memory address
NSString *carInitWithFormat = [[NSString alloc] initWithFormat: @"%@", self.carType];
NSLog(@"carInitWithFormat - %p", carInitWithFormat);
和输出:
2013-10-05 09:45:01.667 TestApp[91103:a0b] self.carType - 0xa084910
2013-10-05 09:45:01.668 TestApp[91103:a0b] carInitString - 0xa084910
2013-10-05 09:45:01.668 TestApp[91103:a0b] carCopy - 0xa084910
2013-10-05 09:45:01.668 TestApp[91103:a0b] carInitWithFormat - 0xa336b70
为什么 carInitString 和 carCopy 不是不同的内存地址?在项目构建设置中关闭了优化。