views:

656

answers:

4

Hi
I made a plist that looks like that:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList 1.0.dtd">
<plist version="1.0">
<array>
<array>
    <dict>
        <key>Company</key>
        <string>xxx</string>
        <key>Title</key>
        <string>VP Marketing</string>
        <key>Name</key>
        <string>Alon ddfr</string>
    </dict>
    <dict>
        <key>Name</key>
        <string>Adam Ben Shushan</string>
        <key>Title</key>
        <string>CEO</string>
        <key>Company</key>
        <string>Shushan ltd.</string>
    </dict>
</array>
<array>
    <dict>
        <key>Company</key>
        <string>xxx</string>
        <key>Title</key>
        <string>CTO</string>
        <key>Name</key>
        <string>Boaz frf</string>
    </dict>
</array>
</array>
</plist>

Now I want to extract the data like that (all the 'A' for key "Name" to one section and all the 'B' "Name" to other one):

NSString *plistpath = [[NSBundle mainBundle] pathForResource:@"PeopleData" ofType:@"plist"];
NSMutableArray *attendees = [[NSMutableArray alloc] initWithContentsOfFile:plistpath];


listOfPeople = [[NSMutableArray alloc] init];//Add items

NSDictionary *indexADict = [NSDictionary dictionaryWithObject:[[attendees objectAtIndex:0] objectForKey:@"Name"] forKey:@"Profiles"];


NSDictionary *indexBDict = [NSDictionary dictionaryWithObject:[[attendees objectAtIndex:1] objectForKey:@"Name"]  forKey:@"Profiles"];

[listOfPeople addObject:indexADict];
[listOfPeople addObject:indexBDict];

This in order to view them in sectioned tableView.

I know that the problem is here:

NSDictionary *indexADict = [NSDictionary dictionaryWithObject:[[attendees objectAtIndex:0] objectForKey:@"Name"] forKey:@"Profiles"];

But I just can't figure how to do it right.

Thanks.

A: 

You have a plist containing an array of arrays of dictionaries.

NSDictionary *indexADict =  (NSDictionary *)[(NSArray *)[attendees objectAtIndex:0] objectAtIndex:0];

The key (Profiles) you are requesting doesn't exist in your plist. There is no point in constructing a new dictionary to hold the existing dictionary from your plist.

If you have an array of dictionaries (or any key value coding compliant objects) you can extract an array of "Name" attributes by:

NSArray *names = [array valueForKey:@"Name"];
Paul Lynch
Thanks, But it shouldnt be something like that: NSDictionary *indexADict = [NSDictionary dictionaryWithObject:[(NSArray *)[attendees objectAtIndex:0] objectAtIndex:0] forKey:@"Name"] ;at the end I want to get only the names for my dictionary in order to view them in sectioned tableView like this: NSDictionary *dictionary = [listOfPeople objectAtIndex:indexPath.section]; NSArray *array = [dictionary objectForKey:@"Name"]; NSString *cellValue = [array objectAtIndex:indexPath.row]; cell.primaryLabel.text = cellValue;
Boaz
the edit styles doesn't works in comment?? I posted below the same comment
Boaz
Response edited into previous answer.
Paul Lynch
A: 

Thanks,
But it shouldnt be something like that?:

    NSDictionary *indexADict = [NSDictionary dictionaryWithObject:[(NSArray *)[attendees objectAtIndex:0] objectAtIndex:0] forKey:@"Name"] ;

at the end I want to get only the names for my dictionary in order to view them in sectioned tableView like this:

    NSDictionary *dictionary = [listOfPeople objectAtIndex:indexPath.section];
    NSArray *array = [dictionary objectForKey:@"Name"];
    NSString *cellValue = [array objectAtIndex:indexPath.row];
    cell.primaryLabel.text = cellValue;
Boaz
Declaring something as a NSArray won't make it so. objectForKey: returns id, so you can cast it to anything, but what you would have got there would be an NSString.
Paul Lynch
Thanks but im still not understand it.. I posted detailed info below
Boaz
A: 

I'm sorry for asking again, I just don't understand it yet.
This is the code I had BEFORE the Plist:
listOfPeople = [[NSMutableArray alloc] init];//Add items NSArray *indexA = [NSArray arrayWithObjects:@"Alon Alroy", @"Adam Ross", nil]; NSDictionary *indexADict = [NSDictionary dictionaryWithObject:indexA forKey:@"Profiles"];

NSArray *indexB = [NSArray arrayWithObjects:@"Boaz Katz", nil];
NSDictionary *indexBDict = [NSDictionary dictionaryWithObject:indexB forKey:@"Profiles"];

[listOfPeople addObject:indexADict];
[listOfPeople addObject:indexBDict]; 

and then i put it inside the viewtable like that:

NSDictionary *dictionary = [listOfPeople objectAtIndex:indexPath.section];
NSArray *array = [dictionary objectForKey:@"Profiles"];
NSString *cellValue = [array objectAtIndex:indexPath.row];
cell.primaryLabel.text = cellValue;

Now I went and built the Plist above and tried to implement it so I will get the same results..
How should I extract the data from the Plist so the rest of the code will work the same? (I have a lot of other functions such as searching that using the sane data and I dont wont to recode them). Thanks A lot for the willing to help.

Boaz
A: 

Hi !

I'm beginner and I try to understand... HOW I can make a request to show all data (from plist) in SAME category (NOT ALL !) for exemple : show only the movie have : genre = Action

Please help me ... Thanks a lot ! Excuse my bad english !

Is it in this code I must make it or an other ???

  • (id)initWithLibraryName:(NSString *)libraryName { if (self = [super init]) { libraryPlist = libraryName; libraryContent = [[NSArray alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:libraryPlist ofType:@"plist"]];
Fraise