【问题标题】:How to store { "Abc" :"Xyz" } in NSDictionary in Objective C如何在Objective C的NSDictionary中存储{“Abc”:“Xyz”}
【发布时间】:2016-06-24 06:38:04
【问题描述】:

如何将下面给出的存储到 NSDictionary

{

“测试”:“示例”

}

【问题讨论】:

  • NSmutableDictionary *info = [NsmutableDictionary allo]init]; [信息 setObject:passValue forKey:passKeyValue]; // like [info setObject:@"Example" forKey:@"Test"];
  • NSDictionary *dict = @{@"Test":@"Example"}; ?

标签: objective-c ios8.3


【解决方案1】:

您可以使用此为键添加值

NSDictionary *dictionary=[NSDictionary dictionaryWithObjectsAndKeys:@"Test",@"Example", nil];

【讨论】:

    【解决方案2】:

    您可以使用 'initWithObjects:(objects) forKeys:' 为键添加值

    NSDictionary* dictionary = [[NSDictionary alloc] initWithObjects:@[@"Example",@"Example2"] forKeys:@[@"Test",@"Test2"]];
    NSLog(@"%@",dictionary);
    //{
    //    Test = Example;
    //    Test2 = Example2;
    //}
    

    或者你可以使用 NSMutableDictionary :

     NSMutableDictionary *mutableDictionary = [[NSMutableDictionary alloc] init];
    [mutableDictionary setObject:@"Example" forKey:@"Test"];
    [mutableDictionary setObject:@"Example2" forKey:@"Test2"];
    NSLog(@"%@",mutableDictionary);
    //{
    //    Test = Example;
    //    Test2 = Example2;
    //}
    

    这些输出相同。 Also you should take a look

    【讨论】:

      猜你喜欢
      • 2014-02-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多