简体   繁体   中英

How to access an nsarray count in a special case

I have block set up as follows:

    id block = ^(id obj, BOOL *ignored) 
{
  id data = [self.writer dataWithObject:obj];
  NSString *output = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];          
};

When i do the call:

[[obj objectForKey:@"roster] objectForKey:@"players"];

I am looking at an array of players. When I add .count, it cannot find a count because it says count is not a property of id. How can I access the count of this array?

Edit:

This is my full block:

id block = ^(id obj, BOOL *ignored) {
                 id data = [self.writer dataWithObject:obj];

                 NSMutableArray *players = (NSMutableArray*)[[obj objectForKey:@"roster"] objectForKey:@"players"];
                 _players = [NSMutableArray array];

                 //Create player objects then add them to the array.

                 for (int i = 0; i <= players.count; i++)
                 {
                     [_players addObject:[HNPlayerObject newPlayerWithPlayerId:(@"%@",[[[obj objectForKey:@"roster"] objectForKey:@"players"][i] objectForKey:@"id"])FirstName:(@"%@",[[[obj objectForKey:@"roster"] objectForKey:@"players"][i] objectForKey:@"first"]) LastName:(@"%@",[[[obj objectForKey:@"roster"] objectForKey:@"players"][i] objectForKey:@"last"]) Number:[(@"%@",[[[obj objectForKey:@"roster"] objectForKey:@"players"][i] objectForKey:@"number"]) intValue] Team:@"Badgers" Position:@"Unknown" Description:@"" GamesPlayed:[(@"%@",[[[obj objectForKey:@"roster"] objectForKey:@"players"][i] objectForKey:@"gp"]) intValue] Goals:[(@"%@",[[[obj objectForKey:@"roster"] objectForKey:@"players"][i] objectForKey:@"g"]) intValue] Assists:[(@"%@",[[[obj objectForKey:@"roster"] objectForKey:@"players"][i] objectForKey:@"a"]) intValue] Points:[(@"%@",[[[obj objectForKey:@"roster"] objectForKey:@"players"][i] objectForKey:@"pts"]) intValue] PenaltyMinutes:[(@"%@",[[[obj objectForKey:@"roster"] objectForKey:@"players"][i] objectForKey:@"pim"]) intValue] PlusMinus:[(@"%@",[[[obj objectForKey:@"roster"] objectForKey:@"players"][i] objectForKey:@"pm"]) intValue] IsPlaying:NO]];
                     [_playersTableView reloadData];
                 }

                 [_playersTableView reloadData];
             };

I am getting this error: 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 17 beyond bounds [0 .. 16]'

 NSArray *array = (NSArray*)[[obj objectForKey:@"roster] objectForKey:@"players"];

然后,您将可以访问count属性。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM