【问题标题】:unexpected braces in my NSMutableArray我的 NSMutableArray 中的意外大括号
【发布时间】:2023-03-17 01:13:01
【问题描述】:

我将电话号码的NSDictionary 添加到NSMutableArray,如下所示:

NSMutableArray *main_level = [NSMutableArray array];


[main_level addObject:@{@"phone":@[@"052443223",@"763765347647",@"763765347647",@"763765347647",@"763765347647",@"763765347647",@"763765347647"]}];

这是日志打印的内容:

2013-10-14 10:49:27.544 tfv[44111:907] (
    {
    phone =         (
        052443223,
        763765347647,
        763765347647,
        763765347647,
        763765347647,
        763765347647,
        763765347647
    );
}
)

但预期的结果是没有这样的大括号: 当我记录这个NSLog(@"%@",[main_level objectAtIndex:0]);

2013-10-14 10:50:22.539 tfv[44129:907] {
phone =     (
    052443223,
    763765347647,
    763765347647,
    763765347647,
    763765347647,
    763765347647,
    763765347647
);

}

为什么要添加这个大括号,我该如何避免呢?

【问题讨论】:

  • 第一个是记录一个数组,其中包含一个包含数组的字典。第二个是记录一个包含数组的字典。

标签: ios json nsmutablearray nsmutabledictionary


【解决方案1】:
( <-- beginning of array
    { <-- beginning of dictionary
    phone =         ( <-- beginning of phone array
        052443223,
        763765347647,
        763765347647,
        763765347647,
        763765347647,
        763765347647,
        763765347647
        ); <-- end of phone array
    } <-- end if dictionary
) <-- end of array

这只是意味着您正在打印一个数组。即NSLog(@"%@",main_level);

如果您使用NSLog(@"%@",[main_level objectAtIndex:0]);,那么您正在打印字典。

【讨论】:

    【解决方案2】:

    1) 您正在使用 key = phone 将电话号码数组添加到字典中。

    2) 然后你将该字典添加到数组中。

    所以,无论输出是否正确。你只是无法避免那个大括号

    如果你想得到第一个电话号码,那么就这样做,

    NSDictionary *dictOfPhoneNumbers = [[main_level objectAtIndex:0] objectForKey:@"phone"];
    
    for(int i=0; i<dictOfPhoneNumbers.count; i++)
    {
        NSLog(@"Phone = %@",[dictOfPhoneNumbers objectAtIndex:i]);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-22
      • 2015-12-08
      • 2013-04-20
      • 2017-11-20
      • 2017-06-01
      • 2019-06-16
      相关资源
      最近更新 更多