I work "correctly" to popularize an array with key-value coding for an iPhone app I am trying to I've been doing some work, but it's a lot of laughter. Actually I'm parsing an XML document in the set of code-generated models. Suppose XML is of this format:
& lt; Foo & gt; & Lt; Bars & gt; & Lt; Item name = "falcon" /> & Lt; Item name = "swamp" /> & Lt; / Bars & gt; & Lt; / Foo & gt;
The bar that represents the element on my generated object, I have NSMutableArray defined for the sub-node:
@interface bar: NSObject { NSMutableArray * items; } @end
By default when I call set value: forKey: At an instance of the bar, it ends in overwriting an example of NSMutableArray with an example of an object is. To do this work, what I have done now, where this hack is found I changed the name of the array instance variable to be something else, suppose that the name is plural form:
@ Interface bar: NSOJECT {NSMutableArray * item; } @end
This causes the default accessory for setValue: forKey: miss to after that I added this method to the bar implementation:
- (zero) Set Value: Defined for (id) value: (NSString *) key {if ([key isqualToString: @ "item"]) {if (objects == zero) {items = [[NSMUTABELARROL] INST ]; [Maintaining objects]; } [Objects addObject: value]; }}
And everything works! I'm sure there should be a better way to do this! I have read through the key-value coding programming guide, but I should remember something, because I'm not clear how to work for array accessories. I have tried to execute countOf and ObjectInAtIndex: As they point to the KVC programming guide, but also with my NSMutableArray being overwritten with an example type of item
If you want to use a KVC property for an array, you should see the mutableArrayValueForKey: < / Code>
In the documentation should look at your class:
@Interface Bar: NSOBK {NSMutableArray * items; } @end
You will minimize these methods, and assume that you have started your Array elsewhere:
- (zero ) Insert object: (id) object InItemsAtIndex: (NSUInteger) index {[item insertObject: index on object: index]; } - (Zero) Remove Object FreemedEmax: (NSUntegaeer) Index {[Object deletion Objective Index: Index]; } - (NSArray *) Item {/ * How do you probably want to come back based on the Pentecost [NSArray arrayWithArray: items] * / return items; }
Once you implement these methods, you can call [bar mutableArrayValueForKey: @ "items"]
. This will return a NSMutableArray
proxy object that you can add and remove objects. That object will in turn generate useful KVO messages and will use the methods to interact with your actual array.
Comments
Post a Comment