I want to expose an atomic property in my obj-c class, but internally I’d prefer to access it’s instance var directly rather using self.myproperty for every reference. How do you manually create a lock to block the external read?
So I want…
@property (atomic, strong) NSString *someString;
- myInternalMethodOfClassA
{
...
@synchronized(someString) {
someString = @"New string";
}
...
}
…to block…
- myInstanceMethodInClientClassB
{
ClassA *myobj = [ClassA alloc] init];
...
NSString *str = myobj.someString;
...
}
According to some chat on the Apple Objc-lang list, it’s considered a bug to have an atomic property for which you manually create one accessor and synthesize the other. The alternatives are:
@synchronized(anObject), which you can then use elsewhere in your class’s code.@synthesizethem and always access them via their accessors (ieself.myAtomicPropinternally). This is slower but most likely not a problem in nearly all cases.