Objective-C ARC Error in Overriding ARC Properties

I have the following code:

// MyObject.h #import <Foundation/Foundation.h> @interface MyObject : NSObject @property (nonatomic, readonly) id property; @end // MyObject.m #import "MyObject.h" @interface MyObject () @property (nonatomic, copy, readwrite) id property; @end @implementation MyObject @synthesize property = _property; @end 

This generates the following warnings and compiler errors:

 warning: property attribute in continuation class does not match the primary class @property (nonatomic, copy, readwrite) id property; ^ note: property declared here @property (nonatomic, readonly) id property; ^ error: ARC forbids synthesizing a property of an Objective-C object with unspecified ownership or storage attribute 

However, if I change the reassessment of the class continuation property to get the weak storage qualifier, no warnings or errors are generated. However (disturbing?) The generated code for -[MyObject setProperty:] calls objc_storeStrong , not the expected objc_storeWeak .

I understand that with LLVM 3.1 the default repository for synthesized ivars strong default. I believe my question is this: why does codegen advocate for a headline declaration over my update in an implementation? Secondly, why does he complain when I upgrade as copy , but not weak or assign ?

+4
source share
1 answer

I understand your question below ...

  • You want readwrite myproperty in the member method of the MyObject class.
  • But, I just want to read myproperty from another class.

//MyObject.h

 @interface MyObject : NSObject { @private id _myproperty; } @property (nonatomic, copy, readonly) id myproperty; @end 

//MyObject.m

 #import "MyObject.h" @interface MyObject () // @property (nonatomic, copy, readwrite) id myproperty; // No needs @end @implementation MyObject @synthesize myproperty = _myproperty; - (void)aMethod { _myproperty = [NSString new]; // You can read & write in this class. } @end 

//Ohter.m

 #import "MyObject.h" void main() { MyObject *o = [MyObject new]; o.myproperty = [NSString new]; // Error!! Can't write. o._myproperty = [NSString new]; // Error!! Can't acsess by objective c rule. NSString *tmp = o.myproperty; // Success readonly. (but myproperty value is nil). } 
+3
source

All Articles