【问题标题】:NSMutableDictionary returns empty NSMutableArrayNSMutableDictionary 返回空的 NSMutableArray
【发布时间】:2012-07-31 08:49:53
【问题描述】:

在 mouseDown 和 mouseDragged 处:

locll = [self convertPoint: [event locationInWindow] fromView:nil];
NSValue *locationValuell = [NSValue valueWithPoint:locll];
[vertices addObject:locationValuell];

鼠标上移

NSString *index = [NSString stringWithFormat:@"%d", aIndex++];
[aDict setObject:vertices forKey:index];
NSArray *allKeys = [aDict allKeys];
NSLog(@"dict count: %ld", [allKeys count]);
NSString *index1 = [NSString stringWithFormat:@"%d", aIndex];
NSMutableArray *a = [aDict objectForKey:index1];
NSLog(@"a count:%li", [a count]);

在initWithCoder

int aIndex = 0;

dict count 返回字典中存储了多少对象。它有效。但是后来当我尝试从字典中取回数组时,我检查了数组中有多少对象([a count])并且它返回 0。所以我猜 NSMutableDictionary 清空了我的 NSMutableArray,或者我以错误的方式取回它。

【问题讨论】:

    标签: objective-c nsmutablearray nsmutabledictionary


    【解决方案1】:

    这里

    NSString *index = [NSString stringWithFormat:@"%d", aIndex++];
    

    你增加aIndex使用它(后增量)。因此,如果index 为“0”,那么index1 将为“1”。因此

    NSMutableArray *a = [aDict objectForKey:index1];
    

    返回nil

    【讨论】:

      【解决方案2】:

      在将 aIndex 用于保存 aDict 中的顶点后,您将 aIndex 增加 1。并且在它(aIndex)增加 1 之后访问这个键。当然,你的 aDict 不包含该键的任何数组。

      【讨论】:

        【解决方案3】:

        试试这个:

               NSMutableArray *a=[NSMutableArray arrayWithObjects:[aDict objectForKey:index1], nil];
        

        应该可以了。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-03-24
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多