Working with plist file - objectiveC

Discussion in 'iOS Programming' started by sunsunai, Oct 11, 2015.

  1. sunsunai, Oct 11, 2015
    Last edited by a moderator: Oct 11, 2015

    sunsunai macrumors newbie

    Oct 11, 2015
    Hi, I have a plist file with Root's type is Array, 20 items with type is Dictionary, each item have 3 keys (Name, Position, Number) and thier type is String. I want to get a key Position where's value is "Mid". I just can get all of thier Values not which value i want.

    This is my code till now.

      //get URL from plist file.
        NSString* path = [[NSBundlemainBundle]pathForResource: @"playersList"ofType: @"plist"];
        //Save plist file to an NSMutableArray
        self.playerOfList = [[NSMutableArrayalloc]initWithContentsOfFile: path];
    NSDictionary* playerShow = [[self.playerOfList objectAtIndex:indexPath.row]objectForKey: @"Position"];
        NSLog(@"%@", playerShow);
    Please help, thanks.
  2. JWalker1995, Oct 11, 2015
    Last edited: Oct 11, 2015

    JWalker1995 macrumors regular

    Mar 5, 2011
    The best thing would be to create a new array called midArray that is an array of dictionary's, I would do something like
    NSMutableArray *midArray =[[NSMutableArray alloc] init];
    For (int x = 0, x <= self.playerlist.count, x++){
         If ([[self.playerlist objectForIndex:x]safeItemForKey:"position"] isEqualToString:"mid"]
              [midArray addObject:[self.playerlist objectForIndex:x]];
    Hope this makes sense, sorry wrote it free hand in my phone
  3. sunsunai thread starter macrumors newbie

    Oct 11, 2015
    It really make sense, now i can print all of my player with position Mid. Very thank you. :)

Share This Page