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

    Joined:
    Oct 11, 2015
    #1
    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.

    Code:
      //get URL from plist file.
    
        NSString* path = [[NSBundlemainBundle]pathForResource: @"playersList"ofType: @"plist"];
    
        //Save plist file to an NSMutableArray
    
        self.playerOfList = [[NSMutableArrayalloc]initWithContentsOfFile: path];
    
    Code:
    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

    Joined:
    Mar 5, 2011
    #2
    The best thing would be to create a new array called midArray that is an array of dictionary's, I would do something like
    Code:
    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

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

Share This Page