How do I release memory with ARC using a public readonly attribute? Say I have the following code:
SomeClass.h:
@interface SomeClass : NSObject
@property (readonly, nonatomic, strong) NSArray* someArray;
@end
SomeClass.m:
#import "SomeClass.h"
@implmentation SomeClass
@synthesize someArray = _someArray;
- (void)dealloc {
self.someArray = nil; //causes compiler error because of public readonly
_someArray = nil; //does this correctly release the object?
}
@end
It’s my understanding that the way you dealloc in ARC is setting all strong properties to nil using the getter method. Since the variable is publicly declared “readonly”, then the compiler will not allow the use the getter method. And from what I know of ARC, setting a iVar to nil does to call release in the underlying code. Is this correct?
Thanks for the help!
When you set it to nil you are telling the compiler that you have no further reference to it and it will add the release for you. The fact that its readonly just means outside classes can’t access it but its still a strong reference so you will need to get rid of that reference for it to be properly released. You can’t access it with self.someArray because using self is accessing it through the property name and not the member variable.