gpt4 book ai didi

ios - 如何将 key 添加到NSMutableArray

转载 作者:行者123 更新时间:2023-12-01 17:05:21 26 4
gpt4 key购买 nike

我有一个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"
]
];

最佳答案

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"]);
}

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

关于ios - 如何将 key 添加到NSMutableArray,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36426112/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com