为什么打印出的每次都是1,不是应该[p release] 后就-1的吗
#import "Person.h"
int main(int argc, const char * argv[])
{
@autoreleasepool {
Person *p = [[Person alloc] init];
NSLog(@"number1-------%ld",[p retainCount]);
[p setAge:12];
NSLog(@"number2-------%ld",[p retainCount]);
[p release];
NSLog(@"number3-------%ld",[p retainCount]);
}
return 0;
}
------解决方案--------------------
release必须赋值nil,才能使引用计数为0
#import "Person.h"
int main(int argc, const char * argv[])
{
@autoreleasepool
{
Person *p = [[Person alloc] init];
NSLog(@"number1-------%ld", [p retainCount]);
[p setAge:12];
NSLog(@"number2-------%ld", [p retainCount]);
[p release];
p = nil;
NSLog(@"number3-------%ld", [p retainCount]);
}
return 0;
}