是关于强弱指针的.
#import <Foundation/Foundation.h>
@interface computer : NSObject
@end
#import "computer.h"
@implementation computer
-(void)dealloc{
NSLog(@"%@---销毁了",self);
}
@end
#import <Foundation/Foundation.h>
#import "computer.h"
@interface Person : NSObject
@property NSString *name;
//弱指针book
@property (nonatomic,weak)computer *book;
@end
#import "Person.h"
@implementation Person
-(void)dealloc{
NSLog(@"%@---销毁了",self);
}
@end
#import <Foundation/Foundation.h>
#import "Person.h"
int main(int argc, const char * argv[]) {
@autoreleasepool {
// 问题开关
__weak Person *pWeak = [Person new];
Person *ren = [Person new];
NSLog(@"1.刚创建人手中的电脑地址: = %@",ren.book);
computer *book = [computer new];
NSLog(@"2.刚创建的电脑的地址 = %@",book);
ren.book = book;
//弱 强
/*@property (weak)computer *book;*/
NSLog(@"3.人拿到电脑后,人手中电脑地址: = %@",ren.book);
book = nil; //强指针没了
NSLog(@"4.赋值为空时,电脑的地址: = %@",book);
NSLog(@"5.既然是弱指针,强指针已经不在 = %@",ren.book);
}
return 0;
}
文中一共有两个类
在
// 问题开关
__weak Person *pWeak = [Person new];
这里你注释掉和打开是两种不同的结果,看地址,便知道了.纠结很久,
只能归结与Xcode的Bug了.
头都大了 |
|