key-value-coding

KVC: How to test for an existing key

I need a little help about KVC. Few words about the operating context: 1) The iPhone connects (client) to a webService to get an object, 2) I'm using JSON to transfer data, 3) If the client has exactly the same object mapping I can iterate through the NSDictionary from JSON to store the data in the permanent store (coreData). To do t...

Using valueForKeyPath on NSDictionary if a key starts the @ symbol?

Hi there, 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 format exception, even when trying to escape the @ symbol: This works fine: [[[...

#typedef and KVC in ObjC

I have a class that looks like this: @interface Properties : NSObject { @private NSNumber* prop1; NSNumberBool* prop2; //etc where NSNumberBool is a typedef: // in MyApp_Prefix.pch typedef NSNumber NSNumberBool; I have all the required @property and @synthesize declarations to make prop1 and prop2 properties. Everythin...

"Key-Value Coding" for Java

In Objective-C on Apple there is something called "Key-Value Coding" that allows you to traverse the object graph using strings similar to filesystem paths. There's an informal protocol (i.e. interface) that allows objects to return values based on the "key" they're asked for. e.g. The default is to return the value of a field named by...

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 <Foundation/Foundation.h> @interface Test : NSObject { float rate; float amount; int duration...

Cocoa Key Value Bindings: What are the explanations of the various options for Controller Key?

When I bind a control to an NSArrayController using Interface Builder, there are a variety of options under the "Controller Key" field in the bindings inspector. I understand what "arrangedObjects" is, and I semi-understand what "selection" is, but I'd love to see a really nice explanation of all the options and when to use each one. Th...

Using -setValue:forKey: vs "object.var = ..."

The difference between these two lines of code is that the second is KVO compliant and the first isn't? [person setValue:tempPerson.name forKey:@"name"]; person.name = tempPerson.name; The reason I'm asking is because I need to update 60 attributes on over 500 objects, I don't want KVO notifications for more than a handful of attribut...

.NET container for two-way conversion data?

Hi all, I have conversion tables I need to contain in memory for fast access. Until now I used a simple Hashtable were the Key was the internal code, and the Value was an object holding the external code and other meta-data. Now we need to have a reverse look-up, meaning to get the internal code based on the external code. I could only...

NSManagedObject subclasses and setValuesForKeysWithDictionary:

I am initializing a NSManagedObject subclass using: - (void)setValuesForKeysWithDictionary:(NSDictionary *)keyedValues I am also knowingly giving it the undefined keys, which of course should through an exception. So, I have implemented: - (void)setValue:(id)value forUndefinedKey:(NSString *)key If a key is undefined, I map it to ...

KVC select by criteria

I have a array of objects that selected from core data. I need select from this set subset of object that correspond to condition. How to do it? ...

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 valueForKey:@"myProperty"]; } @catch (NSException * e) { // Key did not exist } Is there a better...

Javascript JSON key value coding. Dynamically setting a nested value.

I'm working on a little library that lets me do some basic key value coding w/ JSON objects. Say I have the following JSON array: var data = { key1: "value1", key2: { nested1: 1, nested2: "wowza!" } }; And I have the following javascript function: var setData = function(path, value) { eval("data." + path + "= value;"); }; And ...

Cocoa binding to single object from an array

I previously posted this question as a comment on a related thread thinking it was simple. That thread is here: http://stackoverflow.com/questions/670202/cocoa-binding-to-a-particular-item-in-an-array-controller/2082309#2082309 The questions relates to (and I'll more fully describe it here) a game I'm building to try and learn objec...

Core Data and Runtime Key Value Coding

I was under the impression that with key-value coding, I could set any undefined attribute on a NSManagedObject at runtime and no exception would be thrown, but it wouldbe a way to hold objects attached to the model that are not in the data model. For example, I have a Foo object that does not have a "bar" attribute. I though that at r...

Objective C -- is there a keypath that will cause an object to return itself?

Given an object foo of class Foo, I want to do the following: NSString *key = @"some key"; id myObj = [foo valueForKey: key]; and have myObj equal to foo. Is there a way to do this without defining a category on Foo? ...

Accessing collection through KVC (to protect collection and be KVO compliant)

I have a class Test which has an array of Foos. I want to provide access to the Foos without exposing the ivar directly. I'm trying to make this KVC compliant (also to pave the way for KVO compliance). I have: Test.h @interface Test : NSObject { NSMutableArray *foos; } @property (readonly, copy) NSMutableArray *foos; @end Test....

What steps should be taken to convert my XML into Core Data objects?

Hi all, I have an XML file which contains lists of stores, a simplified version is below. What I would like help with is some high-level ideas on the simplest ways to move this data into objects for storage in Core Data. I see suggestions around key-value pairs but as you can see in my example below, I have child elements with the same...

What is the KVC Search Pattern for mutableArrayValueForKey?

Hello! 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, mutableArrayValueForKey:. Below I'm going to explain how I understand valueForKey: KVC "getters" to wor...

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 an NSValue-object like this: [theView setValue:[NSValue valueWithSize:mySize] ...

How to integrate KVC in MVC?

So I have an MVC-application in Cocoa. There are some custom views, a controller and a model. Of course, the views need to know some stuff, so they get their data from the controller. However, they do not use accessors in the controller, they use KVC with a keypath that calls right through to the model: // In view.m time = [timeSource v...