Going through the plist to get information

If I have this layer set,

Key           Type         Value
Root          Array
 Item 0       Dictionary  
 -Title       String       Part One
 -Description String       Welcome to part one. Have fun
 Item 1       Dictionary  
 -Title       String       Part Two
 -Description String       Welcome to part two. Fun too.
 Item 2       Dictionary  
 -Title       String       Part Three
 -Description String       Welcome to part three. It free
 Item 3       Dictionary  
 -Title       String       Part Four
 -Description String       It part four. No more

How could I put all the headers in one array and all the descriptions in another?

+3
source share
2 answers

Ohhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh

NSArray * plistContents = [NSArray arrayWithContentsOfFile:pathToPlist];
NSArray * titles = [plistContents valueForKey:@"Title"];
NSArray * descriptions = [plistContents valueForKey:@"Description"];

The secret here is that a call valueForKey:in an array returns a new array of objects containing the result of the call valueForKey:for each thing in the array. And a call valueForKey:in the dictionary can be equivalent to using objectForKey:(if the key you are using is the key in the key-value pair). See the documentation for more information .

: "" , , , -description ( ).

+5

Cocoa

NSArray *items = [[NSArray alloc] initWithContentsOfFile:@"items.plist"];
NSMutableArray *titles = [[NSMutableArray alloc] init];
NSMutableArray *descriptions = [[NSMutableArray alloc] init];

for (NSDictionary *item in items) {
    [titles addObject:[item objectForKey:@"Title"]];
    [descriptions addObject:[item objectForKey:@"Description"]];
}

[items release];

// Do something with titles and descriptions

[titles release];
[descriptions release];
+1

Source: https://habr.com/ru/post/1746009/


All Articles