key-value-coding

Access Objective-C property dynamically using the name of the property

ε祈祈猫儿з 提交于 2019-11-27 14:57:53
I know the string name of a property of an object. How would I go about getting and setting that property using the string? While @weichsel is correct, there is a better way. Use: [anObject valueForKey: @"propertyName"]; and [anObject setValue:value forKey:@"propertyName"]; Obviously, @"propertyName" can be an NSString that is dynamically composed at runtime. This technique is called Key Value Coding and is fundamental to Cocoa. Why this is better is because -valueForKey will do what is necessary to "box" whatever type the property returns into an object. Thus, if the property is of type int ,

Getting array elements with valueForKeyPath

自闭症网瘾萝莉.ら 提交于 2019-11-27 13:34:52
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 this: NSDictionary *address = [NSString stringWithString:[[[dictionary objectForKey:@"Placemark"] objectAtIndex:0] objectForKey:@"address"]]; NSLog(@"%@", [address valueForKeyPath:@"AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality.LocalityName"]); Just wondering if there's a way to shoehorn the objectAtIndex: call into the valueForKeyPath

Key-Value-Observing a to-many relationship in Cocoa

馋奶兔 提交于 2019-11-27 11:51:28
I am trying to get key-value-observing to work for an NSMutableArray. Below is the .h file for MyObservee, the observed class: @interface MyObservee : NSObject { @private int someValue; @private NSMutableArray *someArray; } @property (readwrite,assign) int someValue; - (NSMutableArray *)someArray; @end The class MyObserver implements observeValueForKeyPath:ofObject:change:context:. Here is how I add the observer: MyObservee *moe = [[MyObservee alloc] init]; MyObserver *mobs = [[MyObserver alloc] init]; [moe addObserver:mobs forKeyPath:@"someArray" options:(NSKeyValueObservingOptionNew |

Access Objective-C property dynamically using the name of the property

血红的双手。 提交于 2019-11-26 18:28:21
问题 I know the string name of a property of an object. How would I go about getting and setting that property using the string? 回答1: While @weichsel is correct, there is a better way. Use: [anObject valueForKey: @"propertyName"]; and [anObject setValue:value forKey:@"propertyName"]; Obviously, @"propertyName" can be an NSString that is dynamically composed at runtime. This technique is called Key Value Coding and is fundamental to Cocoa. Why this is better is because -valueForKey will do what is

Observing an NSMutableArray for insertion/removal

前提是你 提交于 2019-11-26 16:59:59
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 array like this: [myObj.theArray addObject:NSString.string]; An observeValueForKeyPath... notification is not sent. However, the following does send the proper notification: [[myObj mutableArrayValueForKey:@"theArray"] addObject:NSString.string]; This is because mutableArrayValueForKey returns a proxy object that takes care of notifying observers.

Getting array elements with valueForKeyPath

十年热恋 提交于 2019-11-26 16:24:54
问题 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 this: NSDictionary *address = [NSString stringWithString:[[[dictionary objectForKey:@"Placemark"] objectAtIndex:0] objectForKey:@"address"]]; NSLog(@"%@", [address valueForKeyPath:@"AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality

Observing an NSMutableArray for insertion/removal

限于喜欢 提交于 2019-11-26 05:00:03
问题 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 array like this: [myObj.theArray addObject:NSString.string]; An observeValueForKeyPath... notification is not sent. However, the following does send the proper notification: [[myObj mutableArrayValueForKey:@\"theArray\"] addObject:NSString.string]

Difference between objectForKey and valueForKey?

自古美人都是妖i 提交于 2019-11-25 23:35:51
问题 What is the difference between objectForKey and valueForKey ? I looked both up in the documentation and they seemed the same to me. 回答1: objectForKey: is an NSDictionary method. An NSDictionary is a collection class similar to an NSArray , except instead of using indexes, it uses keys to differentiate between items. A key is an arbitrary string you provide. No two objects can have the same key (just as no two objects in an NSArray can have the same index). valueForKey: is a KVC method. It