在阅读Apple的文档后,我尝试在Objective-C中提供属性的原子性或非原子性.为此,我创建了一个具有名和姓的Person.
Person.h
@interface Person : NSObject
@property (nonatomic, strong) NSString *firstName;
@property (nonatomic, strong) NSString *lastName;
- (instancetype)initWithFirstName:(NSString *)fn lastName:(NSString *)ln;
@end
Run Code Online (Sandbox Code Playgroud)
Person.m
@implementation Person
- (instancetype)initWithFirstName:(NSString *)fn lastName:(NSString *)ln {
if (self = [super init]) {
self.firstName = fn;
self.lastName = ln;
}
return self;
}
- (NSString *)description {
return [NSString stringWithFormat:@"%@ %@", self.firstName, self.lastName];
}
@end
Run Code Online (Sandbox Code Playgroud)
在另一个类中,我的AppDelegate,我有一个非原子属性,它是Person的一个实例.
@property (strong, nonatomic) Person *p;
Run Code Online (Sandbox Code Playgroud)
在实现文件中,我创建了三个并发队列.在第一个队列中,我读取了属性,在另外两个队列中,我写了不同的person值.
根据我的理解,我可以在我的日志中输出Bob Frost或Jack Sponge,因为我声明我的属性是非原子的.但那并没有发生.我不明白为什么.我错过了什么或误解了什么吗?
- (BOOL)application:(UIApplication *)application …
Run Code Online (Sandbox Code Playgroud) 我知道如果将它分配给类的属性并且在闭包内部使用类的实例属性,则closure
可以创建retain cycles
.但
1)闭包没有分配给类属性,而是作为参数传递给单例的类方法?
2)在这种情况下如何管理内存?
在我的控制器(UIViewController)的方法中,我有类似的东西:
MySingleton.classMethod(parameters ..., completion: { () -> Void in
/**
doing stuff here
*/
})
Run Code Online (Sandbox Code Playgroud)