Key Value Coding is a mechanism for accessing an object’s properties indirectly, using strings to identify properties, rather than through invocation of an accessor method or accessing them directly through instance variables.
Questions tagged [key-value-coding]
390 questions
355
votes
6 answers
Difference between objectForKey and valueForKey?
What is the difference between objectForKey and valueForKey?
I looked both up in the documentation and they seemed the same to me.

Devoted
- 177,705
- 43
- 90
- 110
76
votes
7 answers
Observing an NSMutableArray for insertion/removal
A class has a property (and instance var) of type NSMutableArray with synthesized accessors (via @property). If you observe this array using:
[myObj addObserver:self forKeyPath:@"theArray" options:0 context:NULL];
And then insert an object in the…

Adam Ernst
- 52,440
- 18
- 59
- 71
55
votes
6 answers
Getting string from Swift 4 new key path syntax?
How can you get a string value from Swift 4 smart keypaths syntax (e.g., \Foo.bar)? At this point I'm curious about any way at all, does not matter if it's complicated.
I like the idea of type information being associated with smart key path. But…

Dannie P
- 4,464
- 3
- 29
- 48
46
votes
4 answers
How do you tell if a key exists for an object using Key-Value Coding?
I'd like to test whether an object has a writeable @property in the iPhone SDK.
One possible way of doing this is to check the -valueForKey: method, but that seems rather inelegant!
Example:
@try {
id *value = [instance…

Ted
- 3,240
- 2
- 17
- 11
31
votes
3 answers
What is the right choice between NSDecimal, NSDecimalNumber, CFNumber?
I've read a lot about NSDecimal, NSNumber, NSNumberDecimal, CFNumber... and it begins to be a kind of jungle to me.
Basically, I'm trying to create a simple model class that will handle simple computations, like this one:
#import…

charlax
- 25,125
- 19
- 60
- 71
26
votes
2 answers
Using setValue(value, forKey: key) on Int? types fires non key value coding-compliant method
I'm successfully using the setValue(value, forKey: key) method in my NSKeyValueCoding compliant Swift NSObject subclass.
This works perfectly well on String optionals, e.g.
var name:String?
However, on Int optionals, it fails, triggering the…

Max MacLeod
- 26,115
- 13
- 104
- 132
25
votes
3 answers
Directly accessing nested dictionary values in Objective-C
Is there a way to directly access an inner-array of an an outer array in Objective-C? For example, a call to an external data source returns the following object:
{
bio = "this is the profile.bio data";
"first_name" = John;
"last_name" =…

user455889
- 793
- 2
- 9
- 14
25
votes
2 answers
Access Objective-C property dynamically using the name of the property
I know the string name of a property of an object. How would I go about getting and setting that property using the string?

quano
- 18,812
- 25
- 97
- 108
24
votes
1 answer
How can I get all values for specific key from each NSDictionary in an NSArray?
I have an array which contains dictionary objects. In each dictionary the key are common. Now I want to get all the values of that key. I have got these values with iteration, but I am looking for some straight forward way or a default method which…

hgpl
- 869
- 4
- 11
- 20
23
votes
5 answers
Getting array elements with valueForKeyPath
Is there any way to access an NSArray element with valueForKeyPath? Google's reverse geocoder service, for example, returns a very complex data structure. If I want to get the city, right now I have to break it into two calls, like…

jsd
- 7,673
- 5
- 27
- 47
20
votes
1 answer
How does NSValue do its magic?
I have an MVC application. The model has a property that is a struct NSSize. It is writable like this:
- (void)setSize:(NSSize)aSize;
The view sets this NSSize using key-value-coding. However, you can not key-value-code a struct, so I wrapped it in…

bastibe
- 16,551
- 28
- 95
- 126
19
votes
8 answers
iPhone app crashing with NSUnknownKeyException setValue:forUndefinedKey:
I'm writing my first iPhone app, so I haven't gotten around to figuring out much in the way of debugging.
Essentially my app displays an image and when touched plays a short sound.
When compiling and building the project in XCode, everything builds…

btw
- 7,006
- 9
- 40
- 40
19
votes
5 answers
Using valueForKeyPath on NSDictionary if a key starts the @ symbol?
I want to use valueForKeyPath on my NSDictionary, but the problem is that one of the keys is a string that starts with the @ symbol. I have no control over the naming of the key.
I'm having problems trying to create the key path as I'm getting a…

Michael Waterfall
- 20,497
- 27
- 111
- 168
16
votes
1 answer
What is the KVC Search Pattern for mutableArrayValueForKey?
I'm attempting to understand Cocoa's Key-Value Coding (KVC) mechanism a little better. I've read Apple's Key-Value Programming Guide but am still a little confused about how certain KVC methods search for keys. Particularly,…

Dave
- 12,408
- 12
- 64
- 67
15
votes
2 answers
Defining your own key path operators in cocoa
Is it possible to define your own key path operators, such as @avg, @sum, etc…

cfischer
- 24,452
- 37
- 131
- 214