• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Navigating property lists using paths?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Navigating property lists using paths?


  • Subject: Re: Navigating property lists using paths?
  • From: p3consulting <email@hidden>
  • Date: Sun, 22 Jun 2003 18:59:21 +0200

> I have a question. I have to navigate deep into a property list (about
> 9 - 10 levels). Can do this with a path string? Similar to
> "\foo\bar\i\am".. What class would I want to do this with? Right now i
> am create a dictionary for each level.
>

Don't be sure that the following answer fully your question but here is
an example of NSDictionary extension that does just that,
except that here the path separator is a dot, [] are used for array
indices:

@implementation NSDictionary(GetObjectForKeyPath)

// syntax of path similar to Java: record.array[N].item
// items are separated by . and array indices in [], array of array of
 of array supported
// example: a.b[N][M].c.d

- (id)objectForKeyPath:(NSString *)inKeyPath
{
NSArray *components = [inKeyPath componentsSeparatedByString:@"."] ;
int i, j, n = [components count], m ;
id curContainer = self ;

for (i=0; i<n ; i++) {
NSString *curPathItem = [components objectAtIndex:i] ;
NSArray *indices = [curPathItem componentsSeparatedByString:@"["] ;

m = [indices count] ;
if (m == 1) { // no [ -> object is a dict or a leave
curContainer = [curContainer objectForKey:curPathItem] ;
}
else {
// indices is an array of string "arrayKeyName" "i1]" "i2]" "i3]"
// arrayKeyName equal to curPathItem

if (![curContainer isKindOfClass:[NSDictionary class]])
return nil ;

curContainer = [curContainer objectForKey:curPathItem] ;

for(j=1;j<m;j++) {
int index = [[indices objectAtIndex:j] intValue] ;

if (![curContainer isKindOfClass:[NSArray class]])
return nil ;

curContainer = [curContainer objectAtIndex:index] ;
}
}

}

return curContainer ;
}

@end

Pascal Pochet
P3 Consulting
email@hidden
http://www.p3-consulting.net
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.

  • Prev by Date: Re: Develop with a Power/i Book
  • Next by Date: NSArray to argv-style string vector. How ?
  • Previous by thread: Re: Navigating property lists using paths?
  • Next by thread: Re: NSToolbarItem Menu
  • Index(es):
    • Date
    • Thread