key-value-coding

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

北城以北 提交于 2019-12-04 17:29:29
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 { NSMutableArray *item; } @end So by default when I call setValue:forKey: on an instance of Bar, it ends up

KVC with NSArrays of NSArrays

雨燕双飞 提交于 2019-12-04 13:20:31
I have an array of arrays that I want to use KVC on (at least I think I do -- it seems like the most straightforward way) but I can't figure out how to create keypaths for individual array indexes. My array looks like this NSArray [ NSArray[0, 1, 2, 3], NSArray[4, 5, 6, 7], NSArray[8, 9, 10, 11] ] What I want to do is get the maximum value of index 3 in the inner array. It seems like something like [outerArray valueForKey:@"@max.[3]"] would work, but I can't figure out the syntax, and my Googling has been fruitless as well. Is what I'm trying to do even possible, or should I just write a

How can I use KVO on SKSpriteNode position property

冷暖自知 提交于 2019-12-04 12:30:36
I would like to use KVO to observe changes to the SKSpriteNode position property, but it doesn't seem to work. Is SKNode's position property observable? Nope, it is not. SK behind the scenes is a C++ engine that bypasses most of Objective-C's overhead, like for instance KVO (KVC will still work cause that's entirely on you). Unlike in UI(Kit) apps, most values of most objects change often anyway, so if you're interested in changes to a property it's faster to simply check it every frame using SKScene's update: or (better) didSimulatePhysics methods. 来源: https://stackoverflow.com/questions

Getting string from Swift 4 new key path syntax?

寵の児 提交于 2019-12-04 09:55:41
问题 How can you get a string value from Swift 4 smart keypaths syntax (e.g., \Foo.bar )? At this point I'm curious about any way at all, does not matter if it's complicated. I like the idea of type information being associated with smart key path. But not all APIs and 3rd parties are there yet. There's old way of getting string for property name with compile-time validation by #keyPath() . With Swift 4 to use #keyPath() you have to declare a property as @objc , which is something I'd prefer to

Performance hit incurred using NSMutableDictionary vs. NSMutableArray>

喜夏-厌秋 提交于 2019-12-04 09:36:23
I am considering using an NSMutableDictionary in place of my current NSMutableArray. This is primarily for KVC/KVO reasons. The collection will undergo heavy mutation within the inner loop of my drawing method. Can I expect to incur a significant performance hit if I go ahead with this replacement? Cheers, Doug The only way to be sure is to measure. None of us have enough knowledge about how NSMutableDictionary's and NSMutableArray's implementations work, so there's little point asking. Granted, you could probably expect some hit since the dictionary has to do additional hashing that a simple

How do I attatch a Key value pair to a UIView on iPhone?

假如想象 提交于 2019-12-04 09:29:47
问题 When I started iPhone development, I read somewhere that it's possible to attach a key value pair to a UIView. I understood that all UIViews could be used as dictionaries to store any data you may want to attatch to them to prevent unnecessary subclassing. However, I've searched everywhere to find the reference and tried to implement the behavior myself in vain. I've tried things such as: UIView *myView = [[UIView alloc] init]; [myView setValue:@"hello" forKey:@"world"]; but this doesn't seem

Properties on CALayer subclass aren't getting observed by CATransaction

谁说胖子不能爱 提交于 2019-12-03 17:18:36
I have a subclass of CALayer with a custom property, declared as such: @interface MyLayer : CALayer @property (nonatomic,retain) NSNumber *customValue; @end @implementation MyLayer @synthesize customValue = _customValue; @end I want to animate this property inside of an explicit CATranasction , so i set up a delegate with the actionForLayer:forKey: method implemented which returns an animation, however any changes to someMyLayerInstance.customValue inside of [CATransaction begin] ... [CATransaction end] do not result in actionForLayer:forKey getting called with a corresponding 'key' value.

KVO differentiating between willChangeValueForKey and didChangeValueForKey - are both necessary?

吃可爱长大的小学妹 提交于 2019-12-03 14:47:46
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 for each half - and, again, with identically the same dictionary contents, save only the difference

How can I get all values for specific key from each NSDictionary in an NSArray? [duplicate]

▼魔方 西西 提交于 2019-12-03 05:42:01
问题 This question already has an answer here : How to get values from NSDictionaries inside an NSArray (1 answer) Closed 6 years ago . I have an array which contains dictionary objects. In each dictionary the key are common. Now I want to get all the values of that key. I have got these values with iteration, but I am looking for some straight forward way or a default method which does this job. Can you please help me to get one default method which serves the purpose? Thanks. Data Structure is

How do you tell if a key exists for an object using Key-Value Coding?

半世苍凉 提交于 2019-12-03 05:31:59
问题 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 way of doing this? 回答1: If you are creating the object that is being checked, you could override valueForUndefinedKey: and setValue:forUndefinedKey to do something more useful