//Person.h
@interface Person : NSObject
//年龄
- (int)age;
@end
//Person.m
@implementation Person
- (int)age
{
return 18;
}
@end
//调用地方
Person * p = nil;
NSLog(@"my age is :%d",[p age]);
Person * p2 = [[Person alloc] init];
[p2 release];
//PS:XXXXXX 对一个object做了release之后,这个object的引用计数会立即减1,但这个object并不一定就立即被free了。直到其引用计数变成0的时候,它才可能真正被free掉在没 free 之前 调用age前加一句log 就是等free了再调用 age 才会crash. 使用 sleep(10)效果是一样的,是不是有同学有时候加一句log后就有不同的运行结果可以查看是不是同样问题
NSLog(@"p2 count %ld",[p2 retainCount]);
NSLog(@"my age is :%d",[p2 age]);
Thread 1: EXC_BAD_ACCESS (code=1, address=0x10
//3,调用一个定义了但没有实现的方法
Person * p3 = [Person new];
[p3 run];
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Person run]: unrecognized selector sent to instance 0x101555eb0'
*** First throw call stack: