Questions tagged [key-value-coding]

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.

384 questions
350
votes
5 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
  • 90,341
  • 41
  • 85
  • 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
  • 45,666
  • 17
  • 55
  • 71
43
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,110
  • 2
  • 15
  • 11
40
votes
4 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,096
  • 3
  • 26
  • 46
29
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
  • 20,351
  • 15
  • 55
  • 67
25
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
  • 24,338
  • 10
  • 91
  • 123
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
  • 17,702
  • 24
  • 94
  • 108
24
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" =…
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
  • 859
  • 4
  • 11
  • 19
22
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,565
  • 5
  • 24
  • 42
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
  • 14,886
  • 24
  • 86
  • 118
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
  • 6,456
  • 9
  • 37
  • 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
  • 19,942
  • 26
  • 108
  • 161
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
  • 11,848
  • 11
  • 59
  • 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
  • 23,024
  • 36
  • 125
  • 209
1
2 3
25 26