一道经典的 NSString 内存面试题

16 阅读1分钟

1/3,

@property (nonatomic, strong) NSString* strA; 
@property (nonatomic, strong) NSString* strB;

self.strA = @"strA"; 
self.strB = self.strA; 
self.strA = nil;
NSLog(@"self.strB 的值是%@", self.strB);

// self.strB 的值是strA

2/3,

@property (nonatomic, strong) NSString* strA; 
@property (nonatomic, weak) NSString* strB;

self.strA = @"strA"; 
self.strB = self.strA; 
self.strA = nil;
NSLog(@"self.strB 的值是%@", self.strB);

// self.strB 的值是strA

3/3

@property (nonatomic, strong) NSString* strA; 
@property (nonatomic, weak) NSString* strB;

self.strA = [NSMutableString stringWithString: @"strA"];
self.strB = self.strA; 
self.strA = nil;
NSLog(@"self.strB 的值是%@", self.strB);

// self.strB 的值是(null)