Avoid staircasing by creating keyPath dynamically possible?
Avoid staircasing by creating keyPath dynamically possible?
- Subject: Avoid staircasing by creating keyPath dynamically possible?
- From: Diederik Meijer <email@hidden>
- Date: Sat, 07 Mar 2015 12:02:27 +0100
Hi all,
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?
Although the above code works fine, I would much rather be able to use an abstract (variable) number of nesting levels, instead of the hardcoded 4 I am using now.
Any thoughts would be appreciated, thanks!
Best,
Diederik
_______________________________________________
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