__strong
The default behavior of the arc environments, objects retain.
For example
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
NSObject *obj = [[NSObject alloc] init];
//默认指针被__strong修饰,等价于:__strong NSObject *p = obj;
NSObject *p = obj;
obj = nil;
NSLog(@"obj = %@", p);
}
复制代码
Print results
obj = <NSObject: 0x100706b90>
复制代码
__weak
Will not object to retain, when the object is destroyed, it will automatically point to nil
For example
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
NSObject *obj = [[NSObject alloc] init];
__weak NSObject *p = obj;
obj = nil;
NSLog(@"obj = %@", p);
}
复制代码
Print results
obj = (null)
复制代码
__unsafe_unretained
Will not object to retain, when the object is destroyed, it will still point to the previous memory space (dangling pointers)
For example
#import <Foundation/Foundation.h>
@interface Person : NSObject
@end
@implementation Person
- (void)dealloc {
NSLog(@"--dealloc--");
}
@end
int main(int argc, const char * argv[]) {
Person *obj = [[Person alloc] init];
__unsafe_unretained Person *p = obj;
obj = nil;
NSLog(@"obj = %@", p);
}
复制代码
Print results
--dealloc--
obj = <Person: 0x101c5e9f0>
复制代码
Object was released, but if memory is not reused, it is still able to print out the Person object; this time to visit is the "zombie" objects.
Reproduced in: https: //juejin.im/post/5cf4ae26f265da1bc07e2352