Re: Avoid staircasing by creating keyPath dynamically possible?
Re: Avoid staircasing by creating keyPath dynamically possible?
- Subject: Re: Avoid staircasing by creating keyPath dynamically possible?
- From: Diederik Meijer <email@hidden>
- Date: Sat, 07 Mar 2015 13:34:47 +0100
Thanks Ken, but this does not seem to traverse down into n nested levels, correct? The loop goes through the nodeTree's first level children only (pardon the bad semantics here)
The aim is to add text sections to the appropriate items array, which can either be a chapter, a chapter's section or a chapter's section's subsection and so on... It is a multilevel table of contents
This is why I need to expand the keyPath, so as to reach the appropriate nested level...
But thanks for your thoughts, highly appreciated!
Sent from my iPhone
> On 07 Mar 2015, at 12:36, Ken Thomases <email@hidden> wrote:
>
>> On Mar 7, 2015, at 5:02 AM, Diederik Meijer <email@hidden> wrote:
>>
>> This is part of XML parsing with NSXMLParser, is there any way to avoid this type of staircasing by constructing a keyPath dynamically?
>>
>> if (self.nestingLevel == 1) { [[[self.rootparser nodeTree] lastObject][@"items"] addObject:dict]; }
>> if (self.nestingLevel == 2) { [[[[self.rootparser nodeTree] lastObject][@"items"] lastObject][@"items"] addObject:dict]; }
>> if (self.nestingLevel == 3) { [[[[[self.rootparser nodeTree] lastObject][@"items"] lastObject][@"items"] lastObject][@"items"] addObject:dict]; }
>> if (self.nestingLevel == 4) { [[[[[[self.rootparser nodeTree] lastObject][@"items"] lastObject][@"items"] lastObject][@"items"] lastObject][@"items"] addObject:dict]; }
>>
>> As you can see the action is always the same: addObject:dict
>>
>> But depending on the level of nesting, lastObject][@"items”] needs to be added multiple times to the self.rootparser’s nodeTree property.
>>
>> Is there any way to use a loop that runs for self.nestingLevel times and adds the extra levels to the keyPath just as many times as needed and then use that to access the noteTree at the exact right level?
>
> No. Key paths can't index into arrays. However, you can just use a simply for loop. Something like:
>
> NSMutableArray* array = [[self.rootparser nodeTree] lastObject][@"items"];
> for (int i = 1; i < self.nestingLevel; i++)
> array = array.lastObject[@"items"];
> [array addObject:dict];
>
> Regards,
> Ken
>
>
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden