How can I use key-value coding messages (setValue:ForKey: and so on) with id
type? Key-value coding protocol is implemented as an informal protocol, so it is not included in <NSObject>
protocol and I can't simply say id<NSObject> setValue:ForKey:
.
Asked
Active
Viewed 94 times
0
1 Answers
1
Just call the method. The compiler won't emit warnings for unknown methods on id
variables.
id myObj = [myArray objectAtIndex:0];
[myObj setValue:@"value" forKey:@"key"];

Ole Begemann
- 135,006
- 31
- 278
- 256
-
Hi, thanks for quick reply. I was using `id
setValue:ForKey:` and `MyProtocol` inherits `NSObject`. In this case compiler emits warnings. Is it possible to find workaround here? I thought it would be better to say 'hey this id means MyProtocol kind of objects' in code. – Nik Nov 18 '11 at 14:09 -
Ah, I see. Yeah, the compiler's behavior toward `id
` is a little strange IMO. In that case, the only (dirty) workaround I see to get rid of the compiler warning is to actually redeclare the `setValue:forKey:` method (and others you want to use) in your protocol. – Ole Begemann Nov 18 '11 at 14:51 -
1Or redeclare the variable without the protocol specifier. – Peter Hosey Nov 18 '11 at 16:10