我该如何处理这个 Array 对象? (目标-C)

Posted

技术标签:

【中文标题】我该如何处理这个 Array 对象? (目标-C)【英文标题】:How can I handle this Array object? (Objective-C) 【发布时间】:2014-03-06 11:36:48 【问题描述】:

首先我有一个NSDictionary。我执行NSURLRequest 并使用请求中的 JSON 信息加载字典。

NSDictionary *json =[NSJSONSerialization JSONObjectWithData:response 
                                                    options:kNilOptions 
                                                      error:&error];

此词典有 2 个键和 2 个值。 1 Key = (_NSCFString) "games" (value: (_NSCFArray*)) 另一个是"id"(玩家的)。 因为我想要我最近做的游戏[json objectForKey:@"games"]。现在我得到一个带有 10 个对象的 NSArray。下面列出了第一个对象。 我如何告诉我的标签玩家玩过哪个英雄(来自 ChampionID)或玩过哪种游戏类型?因为这一切都在 1 个对象中。

如何拆分数组以获取所有信息?

感谢您的每一次帮助。 感谢您的宝贵时间。

'NSLogged' [ objectAtIndex:0]


championId = 102;
createDate = 1394092919791;
fellowPlayers =     (
            
        championId = 37;
        summonerId = 23497430;
        teamId = 200;
    ,
            
        championId = 81;
        summonerId = 29757887;
        teamId = 200;
    ,
            
        championId = 75;
        summonerId = 37419112;
        teamId = 100;
    ,
            
        championId = 64;
        summonerId = 22985242;
        teamId = 100;
    ,
            
        championId = 412;
        summonerId = 50506867;
        teamId = 100;
    ,
            
        championId = 77;
        summonerId = 30770067;
        teamId = 200;
    ,
            
        championId = 61;
        summonerId = 38231891;
        teamId = 200;
    ,
            
        championId = 110;
        summonerId = 46318423;
        teamId = 100;
    ,
            
        championId = 238;
        summonerId = 51117008;
        teamId = 100;
    
);
gameId = 1359691076;
gameMode = CLASSIC;
gameType = "MATCHED_GAME";
invalid = 0;
level = 30;
mapId = 1;
spell1 = 4;
spell2 = 14;
stats =     
    assists = 3;
    championsKilled = 4;
    goldEarned = 12280;
    goldSpent = 11945;
    item0 = 1055;
    item1 = 3153;
    item2 = 3075;
    item3 = 3143;
    item4 = 3047;
    item5 = 1011;
    item6 = 3340;
    killingSprees = 2;
    largestKillingSpree = 2;
    largestMultiKill = 1;
    level = 17;
    magicDamageDealtPlayer = 68999;
    magicDamageDealtToChampions = 5146;
    magicDamageTaken = 2044;
    minionsKilled = 226;
    neutralMinionsKilled = 13;
    neutralMinionsKilledEnemyJungle = 12;
    neutralMinionsKilledYourJungle = 1;
    numDeaths = 1;
    physicalDamageDealtPlayer = 71900;
    physicalDamageDealtToChampions = 2948;
    physicalDamageTaken = 19358;
    team = 200;
    timePlayed = 1814;
    totalDamageDealt = 141513;
    totalDamageDealtToChampions = 8708;
    totalDamageTaken = 21469;
    totalHeal = 3243;
    totalTimeCrowdControlDealt = 276;
    totalUnitsHealed = 1;
    trueDamageDealtPlayer = 614;
    trueDamageDealtToChampions = 614;
    trueDamageTaken = 66;
    turretsKilled = 4;
    wardPlaced = 8;
    win = 1;
;
subType = "CAP_5x5";
teamId = 200; 

更新 1: self.getRecentGames 返回一个 NSDictionary(它不是 self.recentGames 的 getter)

    self.recentGames = [[NSDictionary alloc] initWithDictionary:self.getRecentGames];
    NSDictionary *dict = [self.recentGames objectForKey:@"games"];
    //now I my dict have 10 object but dict is from type: __NSCFArray

更新 2:完成! 非常感谢您的宝贵时间。在我检查了数组中的对象显示哪个类之后,它是一个 NSDictionary。 就这么简单,我就能找出来。 您的回答很有帮助!

【问题讨论】:

每个数组对象都是NSDictionary,对吧?您应该能够使用以下方式访问内部数据:[((NSDictionary *)[games objectAtIndex:0]) valueForKey:@"championId"]; 你有标签数组吗?目前尚不清楚您到底在努力解决什么问题 - 详细信息列表,简单地从字典中获取项目...... 读取转储。 包围一个字典,() 包围一个数组。一次剥一层。如果需要,请编写一个循环来搜索您的数组。 (是的,有更好的方法可以在一个语句中进行搜索,但是如果你编写循环,你会明白你在做什么更好。) 【参考方案1】:

假设您在问题中显示的结构,您可以执行以下操作:

NSDictionary *game = [games objectAtIndex:0]; // That you did already
NSNumber *championID = [game objectForKey:@"championId"];

根据您的评论进行修改:

就像用户 Hot Licks 提到的那样,如果您遇到这样的异常,您可能会假设您有一本字典,但最终却得到了一个数组。在这种情况下,您需要重复两次:

NSArray *game = [games objectAtIndex:0]; // That you did already
NSNumber *championID = [game objectAtIndex:indexOfChampionId];

虽然我怀疑后者是否是您真正想要的。因此,您可能必须检查导入数据的部分。

【讨论】:

顶部是一个名为 dict 的 NSDictionary。在这个字典中有 2 个键,键“游戏”代表一个 NSArray(最近的 10 场游戏),另一个键不相关。所以当我这样做时 NSDictionary *game = [games objectAtIndex:0]; NSNumber *championID = [game objectForKey:@"championId"]; 我会得到一个错误:-[__NSCFArray objectForKey:]: unrecognized selector sent to instance 0xab6c8e0 2014-03-06 13:55:25.612 *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFArray objectForKey:]: unrecognized selector sent to instance 0xab6c8e0' @IntegerGaming - 这个错误告诉你你有一个数组,而不是字典。回去再看看转储,找出你做错了什么。【参考方案2】:

这里有一些代码可以帮助您解决问题。当然,这只是一个简单的方案,应该可以帮助您解决问题。

这种解决方案的主要思想是递归地创建对象。您必须将所需的所有属性添加到 Game 和 Champion 类中。

 NSArray *json =[NSJSONSerialization JSONObjectWithData:response 
                                                options:kNilOptions 
                                                  error:&error];

 NSMutableArray *games = [NSMutableArray array];
 for (NSDictionary *gameDictionary in json) 
   Game *game = [[Game alloc] initWithJSONDictionary:gameDictionary];
   [games addObject:game];
 

 //games is the table which your game objects




///Game class
@interface Game
   @property (strong, nonatomic) NSArray *champions;

   - (id)initWithJSONDictionary:(NSDictionary *)dictionary;
@end

 @implementation Game
     - (id)initWithJSONDictionary:(NSDictionary *)dictionary
 
   self = [super init];
   if (self) 
      NSMutableArray *array = [NSMutableArray array];
      NSArray *champions = dictionary[@"championId"];
      foreach (NSDictionary *championDictionary in champions) 
          Champion *champion = [[Champion alloc] initWithJSONDictionary:championDictionary];
          [array addObject:champion];

      
      self.champions = array;
   

   return self;
 

 @end

/// Champion class
 @interface Champion 
   @property (strong, nonatomic) NSString *championId;
   @property (strong, nonatomic) NSString *summonerId;
   @property (strong, nonatomic) NSString *teamId;

   - (id)initWithJSONDictionary:(NSDictionary *)dictionary;

 @end

 @implementation Champion

   - (id)initWithJSONDictionary:(NSDictionary *)dictionary
   
     self = [super init];
     if (self) 
       self.championId = dictionary[@"championId"];
       self.teamId = dictionary[@"teamId"];
       self.summonerId = dictionary[@"summonerId"]l
     

     return self;
   
 @end

【讨论】:

以上是关于我该如何处理这个 Array 对象? (目标-C)的主要内容,如果未能解决你的问题,请参考以下文章

我该如何处理这个警告:哈希不匹配?

django MultiValueDictKeyError 错误,我该如何处理

Codeigniter:当我插入数据时出现重复键错误,我该如何处理这个错误?

如果连接关闭,我该如何处理连接并重新连接?

iOS 14 后列表背景颜色更改并添加了填充,我该如何处理?

我有一个分类项目,其中一些列/特征具有超过 90% 的空值。我该如何处理它们?