key-value-coding

Key-Value Coding (KVC) with Array/Dictionary in Swift

怎甘沉沦 提交于 2019-12-18 13:27:11
问题 Is it possible to key-value code (KVC) with native Swift data structures such as Array and Dictionary? Key-Value coding is still available for NSFoundation structures within Swift, just like in Objective C. For example, this is valid: var nsarray: NSArray = NSArray() // Fill the array with objects var array: NSArray = nsarray.valueForKeyPath("key.path") But this is invalid: var swiftarray: Array = [] // Fill the array with objects var array = swiftarray.valueForKeyPath("key.path") // Invalid,

Keypath for first element in embedded NSArray

和自甴很熟 提交于 2019-12-18 03:31:06
问题 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

Directly accessing nested dictionary values in Objective-C

匆匆过客 提交于 2019-12-17 15:37:45
问题 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,

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

自作多情 提交于 2019-12-17 15:32:46
问题 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

Using dot notation for instance methods

雨燕双飞 提交于 2019-12-14 04:25:25
问题 I was looking at a piece of code today and notice that this particular coder use dot notation to access instance methods (these methods don't take values, they just return value). For example: @interface MyClass : NSObject { } -(double)add; @end @implementation MyClass -(double)valueA { return 3.0; } -(double)valueB { return 7.0; } -(double)add { return self.valueA + self.valueB; } @end He did this through out his code and the compiler doesn't complain, but when I try it in my code like the

Accessing constants using Key-Value Coding in Objective-C

眉间皱痕 提交于 2019-12-13 19:22:16
问题 I'm making a program that has a lot of constants. I decided to put them all into a separate class and I'm importing it by the classes that need it. The files look similar to this // Constants.h extern const int baseCostForBuilding; extern const int maxCostForBuilding; // etc // Constants.m const int baseCostForBuilding = 400; const int maxCostForBuilding = 1000; // etc What I'm trying to do is access them using key-value coding. What I've tried so far hasn't worked. id object = [self

How do I change a UIButton's title using KVC?

≡放荡痞女 提交于 2019-12-13 18:35:04
问题 I have an array of UIButtons and want to set all their titles to a specific value at once, without looping through the array. The only solution I've found is by means of Key-Value Coding, i.e. something like this: [self.board setValue:@"X" forKeyPath:@"titleLabel.text"]; However, the button's titleLabel property is readonly and cannot be changed. I also tried using the button's title property as the keypath, but it doesn't work either. I've done this before by changing the "enabled" property

Class is not key value coding compliant

烂漫一生 提交于 2019-12-13 04:24:19
问题 I have a NavigationController , root view controller called "ViewController" and a second view controller called "SettingsViewController". I have a segue to the "SettingsViewController" from "ViewController" which worked until I tried to connect a UITextField called "usernameField" to the "SettingsViewController" by means of Reference Outlet. When I did I got the following error whenever I try to go to the SettingsViewController: *** Terminating app due to uncaught exception

Why doesn't setValue:forKeyPath invoked on mutable dictionary throw exception for unknown keypaths?

微笑、不失礼 提交于 2019-12-12 17:07:59
问题 I have following code: NSMutableDictionary *dict = [NSMutableDictionary dictionary]; [defs setObject:[NSNumber numberWithInt:100] forKey:@"test1.test2.test3"]; [defs setValue:[NSNumber numberWithInt:10] forKeyPath:@"test2.test3.test4"]; I understand that setObject:forKey: creates association between "test1.test2.test3" key and given number object. On the other hand, setValue:forKeyPath: is Key-Value-Coding method that tries to locate object for path "test2.test3.test4", but in the end, it

Cocoa binding to a particular item in an array controller

会有一股神秘感。 提交于 2019-12-12 12:07:28
问题 Is it possible using NSArrayController to bind a NSTextField 's value to a particular item in the array? In particular, I want to bind to a property on the first item in the array, and show nothing if the array is empty. Using arrangedObjects.command shows just "(" -- presumably it's trying to show a multi-line string with comma-separated strings for each item. I just want the first one. 回答1: Bind the text field to selection.command , and programmatically set the array controller's selection