key-value-coding

KVC string conversion not working for BOOL value

依然范特西╮ 提交于 2019-11-29 15:42:39
问题 Hey. I am reading in a string from a file and attempting to use the resulting string to set a BOOL property on an object using the KVC method -setValue:forKeyPath: . However, this breaks with an exception: -[NSCFString charValue]: unrecognized selector sent to instance 0x7fff711023b0 . I'm guessing this is because BOOL is typedef'd from char. Is there a way around this? Thanks! 回答1: When setting a BOOL property using KVC, you need to pass an NSNumber object. What you could do in your case is

Keypath for first element in embedded NSArray

这一生的挚爱 提交于 2019-11-29 01:52:40
This example is contrived, but it shows my point. So, if I have an object graph like the following: { sex = male; uid = 637650940; work = ({ employer = { id = 116420715044499; name = "Software Engineer"; }; "end_date" = "0000-00"; "start_date" = "0000-00"; }, { employer = { id = 188733137832278; name = "Apple"; }; }); }, //Some more objects (This is an NSArray containing NSDictionarys that have an object of type NSArray). The key field is work . I want a Key Path that will take the first object in the work array. If I do this: NSArray* work = [outerArrayObject objectForKey: @"work"]; id name =

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

谁说我不能喝 提交于 2019-11-29 01:21:31
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: [[[dict objectForKey:@"key1"] objectForKey:@"@specialKey"] objectForKey:@"key3"] However none of these work: [dict valueForKeyPath:@"key1.@specialKey.key3"] [dict valueForKeyPath:@"key1.@@specialKey.key3"] Any ideas? Thanks, Mike pxl you shouldn't be using @ signs with your

Javascript object key value coding. Dynamically setting a nested value

廉价感情. 提交于 2019-11-28 22:10:51
I'm working on a little library that lets me do some basic key value coding with objects. Say I have the following object: 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 in use: setData("key1", "updated value1"); // data.key1 == "updated value1" setData("key2.nested1", 99); // data.key2.nested1 == 99 This works, however I would like to accomplish the above without using eval . Is this possible, or is eval the best way to go? EDIT: NOTE it can be

What is the KVC Search Pattern for mutableArrayValueForKey?

▼魔方 西西 提交于 2019-11-28 21:03:50
问题 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 work. Then I'll get to my question regarding mutableArrayValueForKey. There are seven different "getter" KVC methods: - (id)valueForKey:(NSString *)key; - (id

Why is my object not key value coding-compliant?

五迷三道 提交于 2019-11-28 02:25:33
问题 Trying to use key-value coding to set a value on my object: [engine setValue:[NSNumber numberWithInt:horsePower] forKey:@"horsePower"]; Causes an error: [<Slant6 0x7fbc61c15c40> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key horsePower. What do I need to do to make engine key value coding-compliant? 回答1: Make sure whatever class engine belongs to implements a horsePower property, and/or has a horsePower instance variable, and/or manually implements

Javascript object key value coding. Dynamically setting a nested value

不想你离开。 提交于 2019-11-27 21:04:32
问题 I'm working on a little library that lets me do some basic key value coding with objects. Say I have the following object: 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 in use: setData("key1", "updated value1"); // data.key1 == "updated value1" setData("key2.nested1", 99); // data.key2.nested1 == 99 This works, however I would like to

Directly accessing nested dictionary values in Objective-C

天涯浪子 提交于 2019-11-27 19:05:12
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" = Doe; location = { name = "Any Town, Any State"; }; metadata = { pictures = { picture = "https://picture.mysite.com/picture.jpeg"; } } } I want to be able to access, for example, the location.name or the metadata.pictures.picture data. Dot notation, however, does not seem to work. For example: _gfbLocation = [result objectForKey:@"location.name"]; _gfbPicture =

What is the right choice between NSDecimal, NSDecimalNumber, CFNumber?

走远了吗. 提交于 2019-11-27 18:32:25
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; } - (float)capitalizedAmount; @end @implementation Test - (float)capitalizedAmount { return (amount*pow((1.0+rate),duration)); } @end I want to access these methods and setters with their names as strings, since I plan to have a lot of other classes like this one, and I am

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

試著忘記壹切 提交于 2019-11-27 15:50:48
问题 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: [[[dict objectForKey:@"key1"] objectForKey:@"@specialKey"] objectForKey:@"key3"] However none of these work: [dict valueForKeyPath:@"key1.@specialKey.key3"] [dict