gpt4 book ai didi

ios - 字典中每个键的多个值

转载 作者:行者123 更新时间:2023-11-28 19:03:13 25 4
gpt4 key购买 nike

我正在创建一个应用程序,它检索特定国家/地区的地址列表,并按第一个字母将它们分组到字典中。所以 Amsterdam 将被放在键“A”下。

这些地址是从 CSV 文件中读取并迭代的。我遇到的问题是我用键 A-Z 制作了一本字典。

在我的 for 循环中,我想将每个地址添加到正确的键中,但由于它在一个循环中,我一次只有一个地址。 [dictionary setObject] 只会放在最后一个值中。

我应该如何处理这个问题?

最佳答案

如何使用 NSMutableArraysNSDictionary (A-Z)?然后您将能够像这样添加项目:

for (NSString* theName in (...)) {
NSString* firstLetter = [[theName substringToIndex:1] uppercase];

// Check this in case there is 'malformed' input, e.g. strings beginning
// with non-alpha characters
if ([theDictionary objectForKey:firstLetter]) {
[theDictionary[firstLetter] addObject:theName];
} else {
NSLog(@"Skipping item %@", theName);
}
}

你可以像这样初始化theDictionary:

theDictionary = [NSMutableDictionary dictionary];
for (char c = 'A'; c <= 'Z'; c++) {
theDictionary[[NSString stringWithFormat:@"%c", c]] = [NSMutableArray array];
}

关于ios - 字典中每个键的多个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22844371/

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