Problem
I ran into an interesting problem and could not find the documentation for it ... Sometimes the properties declared in protocol are not implemented in a specific class corresponding to this protocol and a runtime exception occurs. Are dynamic property definitions defined in some strange circumstances? Can protocols not be used with properties that was declared as dynamic ? Any understanding of this would be very helpful.
Below are some details.
Given protocol :
@protocol MyProtocol <NSObject> @property (nonatomic, strong) id someProperty; @end
and a class that implements protocol as follows:
@interface MyClass <MyProtocol> @end @implementation MyClass @dynamic someProperty; @end
I noticed that sometimes I canβt get any information from the call
class_getProperty(myClass, propertyName);
for properties in protocol . This only happens with some classes and seems sporadic.
I launch the latest Xcode 4 and get attached to the iOS 6 SDK. I have a preliminary release of Xcode 5 installed on the same computer, although it is not standard (via xcode-select).
Example
If you run this code:
@protocol MyProtocol <NSObject> @property (nonatomic, strong) id someData; @end @interface MyObject : NSObject <MyProtocol> @end @implementation MyObject @dynamic someData; @end
and then you run
const char *name = [@"someData" UTF8String]; objc_property_t property = class_getProperty([MyObject class], name); const char *attributes = property_getAttributes(property);
You will receive the metadata in the property EVEN THOUGH property does not exist. In other words, you do not need to synthesize a property to get its attributes . Runtime still knows this. Try it yourself. The problem is that sometimes this does not happen. I want to know the conditions that cause the runtime to not know property attributes.
Interim fix
My temporary fix is ββto simply copy all the property definitions into the protocol and paste them into the .h file:
@interface MyClass <MyProtocol> @property (nonatomic, strong) id someProperty; @end @implementation MyClass @dynamic someProperty; @end
This works great, although far from ideal. However, this suggests that my code is working correctly, and the problem lies elsewhere.
I would be happy to provide more details or information if necessary.