簡體   English   中英

如何將密鑰添加到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因為數組不包含鍵和值數組僅具有索引

我的代碼將為您提供幫助:)

  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"
  }];
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是NSArrayNSDictionary和attributeKeys是NSArray鑰匙。

請參考以下參考鏈接:

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];

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM