如何将键添加到 NSMutableArray

How to add key to NSMutableArray

我有一个 NSMutableArray 看起来像这样。我想将这个数组传递给另一个 ViewController 并使用它的元素。但我对如何为每个元素添加键感到困惑。例如,{@"bus_route_key":@"733"}

有没有办法用键初始化数组?或者我应该为此创建 NSDictionary 吗?

@[
 @[
  @733,
  @"Oakleigh - Box Hill via Clayton & Monash University & Mt Waverley",
  @"Oakleigh Railway Station/Johnson St",
  @"2016-04-05T11:44:00Z"
  ],
 @[
  @631,
  @"Southland - Waverley Gardens via Clayton & Monash University",
  @"Southland Shopping Centre/Karen St",
  @"2016-04-05T11:46:00Z"
  ],
 @[
  @703,
  @"Middle Brighton - Blackburn via Bentleigh & Clayton & Monash University (SMARTBUS Service)",
  @"Luntar Rd/Centre Rd",
  @"2016-04-05T11:50:00Z"
  ]
 ];

哇,你好墨尔本人。

您应该有一个包含 NSMutableDictionaries 的 NSMutableArray,并设置每条总线以更有条理:

(
  {
    "route": 733,
    "line_name": "Oakleigh - Box Hill via Clayton & Monash University & Mt Waverley",
    "stop": "Oakleigh Railway Station/Johnson St",
    "time": "2016-04-05T11:44:00Z"
  },

  //Insert other line dictionaries
)

目前,您的 NSMutableArray 包含 NSArrays。在您的情况下,这些 NSArray 需要 NSDictionaryNSMutableDictionary

然后您可以像这样编辑第一个元素的 bus_route_key[[yourList objectAtIndex:0] setInteger:1337 forKey:@"bus_route_key"] 如果要设置字符串,需要使用setObject

您可以将 NSArray 转换为 NSDictionary,因为数组不包含键和值数组只有索引

NSArray *mainArray = @[
 @[
  @733,
  @"Oakleigh - Box Hill via Clayton & Monash University & Mt Waverley",
  @"Oakleigh Railway Station/Johnson St",
  @"2016-04-05T11:44:00Z"
  ],
 @[
  @631,
  @"Southland - Waverley Gardens via Clayton & Monash University",
  @"Southland Shopping Centre/Karen St",
  @"2016-04-05T11:46:00Z"
  ],
 @[
  @703,
  @"Middle Brighton - Blackburn via Bentleigh & Clayton & Monash University (SMARTBUS Service)",
  @"Luntar Rd/Centre Rd",
  @"2016-04-05T11:50:00Z"
  ]
 ];

NSMutableArray *newArray = [NSMutableArray array];

for (NSArray *arr in mainArray) {

        NSDictionary *dict = @{
                               @"key1" : [arr objectAtIndex:0],
                               @"key2" : [arr objectAtIndex:1],
                               @"key3" : [arr objectAtIndex:2]
                               };

    [newArray addObject:dict];
}

DLOG(@"New Arr : %@", newArray);

//you can access by key like this

for (NSDictionary *dict in newArray) {

    DLOG(@"%@", [dict valueForKey:@"key1"]);
    DLOG(@"%@", [dict valueForKey:@"key2"]);
    DLOG(@"%@", [dict valueForKey:@"key3"]);
}

希望对您有所帮助..:)

使用下面的代码行:
NSMutableDictionary *dict2 =[[NSMutableDictionary alloc] initWithObjects:attributeValues forKeys:attributeKeys];

这里的 attributeValues 是 NSArray 的值 NSDictionary 而 attributeKeys 是 NSArray 的键。

参考以下参考 link :

http://hayageek.com/nsdictionary-nsmutabledictionary/

NSString * key1 =@"name";
NSString * obj1 =@"Hayagreeva";
NSString * key2 =@"age";
NSNumber * obj2 =[NSNumber numberWithInt:3];

//1.Empty dictionary. @{} dictionary representation and it is not for  NSMuableDictionary
NSDictionary * dict1 =[NSDictionary dictionary];
NSDictionary * dict2 =[[NSDictionary alloc] init];
NSDictionary * dict3 = @{};

//2.Dictionary with Single Object and Key
NSDictionary * dict4 =[NSDictionary dictionaryWithObject:obj1 forKey:key1];
NSDictionary * dict5 =@{key1:obj1};

//3.Dictionary with Multiple Object and Keys
//Objects and keys are sequentially terminated with nil
NSDictionary * dict6 = [NSDictionary
                        dictionaryWithObjectsAndKeys:obj1,key1,obj2,key2,nil];

NSDictionary * dict7 =[[NSDictionary alloc]
                       initWithObjectsAndKeys:obj1,key1,obj2,key2,nil] ;

//3.Dictionary with Multiple Object and Keys
NSDictionary *dict8 =[NSDictionary
                      dictionaryWithObjects:@[obj1,obj2]
                      forKeys:@[key1,key2]];
NSDictionary *dict9 =[[NSDictionary alloc]
                      initWithObjects:@[obj1,obj2]
                      forKeys:@[key1,key2]];
NSDictionary * dict10 =@{key1:obj1,key2:obj2};

//@[obj1,obj2] is the array representation.  dict6,dict7,dict8 are same.
NSDictionary *dict11 =[[NSDictionary alloc]
                       initWithObjects:[NSArray arrayWithObjects:obj1,obj2,nil]
                       forKeys:[NSArray arrayWithObjects:key1,key2,nil]];

//4.Dictionary with another Dictionary
//@[obj1,obj2] is the array representation.  dict6,dict7,dict8 are same.
NSDictionary *dict12=[NSDictionary dictionaryWithDictionary:dict8];
NSDictionary *dict13=[[NSDictionary alloc] initWithDictionary:dict8];

我的代码会帮助你:)

  NSMutableArray *arrTitles = (NSMutableArray *)@[@{
     "route": 733,
     "line_name": "Oakleigh - Box Hill via Clayton & Monash University & Mt Waverley",
     "stop": "Oakleigh Railway Station/Johnson St",
     "time": "2016-04-05T11:44:00Z"
      },@{
    "route": 733,
    "line_name": "Oakleigh - Box Hill via Clayton & Monash University & Mt Waverley",
    "stop": "Oakleigh Railway Station/Johnson St",
    "time": "2016-04-05T11:44:00Z"
  }];