How can I make the objective-c compiler assume undeclared properties are dynamic

De openkb
Aller à : Navigation, rechercher

Sommaire

Questions

I d like to be able to write

id foo = [MyObject new];
foo.bar = [NSObject new];

But I get the following error:

/blah/blah/blah/Blah.m:32:9: error: property  bar  not found on object of type  __strong id 
foo.bar = [NSObject new];
    ^
1 error generated.

Since clang doesn t report a warning I could suppress, I assume this behavior is permanent, but I hope someone can tell me otherwise.

http://developer.apple.com/library/ios/#documentation/cocoa/conceptual/ObjCRuntimeGuide/Articles/ocrtDynamicResolution.html#//apple_ref/doc/uid/TP40008048-CH102-SW1 http://developer.apple.com/library/ios/#documentation/cocoa/conceptual/ObjCRuntimeGuide/Articles/ocrtDynamicResolution.html#//apple_ref/doc/uid/TP40008048-CH102-SW1

Answers

You can t use the dot syntax on a generic object pointer like this.

The reason is that the compiler rewrites foo.bar = baz; to [foo setBar:baz], but since you can change the setter s name, @property (strong, nonatomic, setter=setListOfApples:) NSArray * listOfOranges;, it must be able to see the property declaration.

If you use the standard message send syntax, this will work. In other words, you have to either tell the compiler the exact type of the object so that it can look up the property s setter, or tell it the exact setter name you want it to use.

Source

License : cc by-sa 3.0

http://stackoverflow.com/questions/12712535/how-can-i-make-the-objective-c-compiler-assume-undeclared-properties-are-dynamic

Related

Outils personnels
Espaces de noms

Variantes
Actions
Navigation
Outils