key-value-coding

Decoding/parsing CSV and CSV-like files in Swift

时光怂恿深爱的人放手 提交于 2020-06-01 07:38:07
问题 I'll have to write a very customised CSV-like parser/decoder. I have looked for open source ones on Github, but not found any that fits my needs. I can solve this, but my question is if it would be a total violation of the key/value decoding, to implement this as a TopLevelDecoder in Swift. I have keys, but not exactly key/value pairs. In CSV files, there is rather a key for each column of data, There are a number of problem with the files I need to parse: Commas are not only for separation

Why should I use KVC rather than the simple dot syntax when accessing object properties?

◇◆丶佛笑我妖孽 提交于 2020-01-24 10:14:50
问题 There's the option to go the long way, if an receiver class conforms to the NSKeyValueProtocol: [myInstance setValue:[NSNumber numberWithInt:2] forKey:@"integerProperty"]; or the short way: myInstance.integerProperty = 2; what's the point of this KVC method? When is this useful? 回答1: First, those aren't the same, the second should be: myInstance.integerProperty = [NSNumber numbwerWithInt:2]; if integerProperty is an NSNumber . In general you use the second form when you are doing the most

On Objective-C/Cocoa Key-Value coding and arrays

大憨熊 提交于 2020-01-13 05:47:53
问题 I'm trying to work out the "correct" way to handle populating an array with key-value coding for an iPhone app. I've come up with something that works, but it's fairly hackish. Basically I'm parsing an XML document into a set of code-generated models. Let's assume the XML is of this format: <foo> <bar> <item name="baz" /> <item name="bog" /> </bar> </foo> On my generated object that represents the Bar element, I have an NSMutableArray defined for the sub-node: @interface Bar : NSObject {

KVO differentiating between willChangeValueForKey and didChangeValueForKey - are both necessary?

跟風遠走 提交于 2020-01-12 08:01:06
问题 In line with Apple's own recommendations, when setting KVC/KVO compliant accessors manually, one should include BOTH KVO methods willChange and didChange . This is what I have done in all my manual accessor methods. However, observeValueForKeyPath:ofObject:change:context gets called for each half of the KVC methods (will and did) with exactly the same dictionary contents. When registering an observer using the option: NSKeyValueObservingOptionPrior the observer still gets called twice - once

How to convert NSValue to NSData and back?

生来就可爱ヽ(ⅴ<●) 提交于 2020-01-10 10:14:35
问题 A have a number of NSValue (obtained via KVC valueForKey ) that I need to append to an NSData object in order to send it over the network using Game Center. Obviously I will also need to convert the NSValue back from NSData for more KVC ( setValue:forKey: ). I don't know the exact type of each NSValue , so I'm limited to using the interfaces provided by NSValue and NSData . I should mention that the NSValue are never pointer types. I'm surprised that there's no [NSData dataWithValue:value]

How to Key-Value-Observe the rotation of a CALayer?

感情迁移 提交于 2020-01-04 05:26:14
问题 I can access the value like this: NSNumber* rotationZ = [myLayer valueForKeyPath:@"transform.rotation.z"]; But for some reason, if I try to KV-observe that key path, I get a compiler error. First, this is how I try to do it: [myLayer addObserver:self forKeyPath:@"transform.rotation.z" options:0 context:nil]; The compiler tells me: *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[ addObserver: forKeyPath:@"rotation.z" options:0x0 context:0x528890] was sent to an

“Key-Value Coding” for Java

此生再无相见时 提交于 2019-12-29 04:24:10
问题 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 the key, while relational collections like NSDictionaries can implement more interesting behavior. Pseudo code example: foo.bar = new baz(); foo.bar.mymap =

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

谁都会走 提交于 2019-12-28 03:38:11
问题 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

Limiting the number of objects in NSArrayController

隐身守侯 提交于 2019-12-25 00:20:57
问题 I'm trying to create some kind of "Top 25" list in my app. I've used NSPredicate to filter the contents of the array controller but I want to limit the number of the results to just 25 objects. How could I do that? 回答1: Another strategy would be to subclass NSArrayController and override arrangedObjects to return something like [[super arrangedObjects] subarrayWithRange:NSMakeRange( 0, 25 )]; (you would probably want to check the length of the array first). Of course this array controller

How can I get int values from the change dictionary in KVO method observeValueForKeyPath:ofObject:change:context:?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-24 13:20:05
问题 I am observing changes in the rate property of an AVPlayer by calling the following method on it like so: addObserver:sharedPlayerManagerInstance forKeyPath:@"rate" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld context:0]; The change dictionary I get back from observeValueForKeyPath:ofObject:change:context: is therefore: change = { kind: 1, new: 1, old: 0 } I checked the class of each value, and it turns out to be __NSCFNumber. However, when I try to convert [change