iOS:如何从此 for 循环中获取每个字符串并将其添加到 NSMutableArray 中?

标签 ios objective-c dictionary nsmutablearray nsarray

我有一个 for 循环,它在完成后会给我两个 string。但是我需要将这两个 string 添加到 array 中。第一个 stringone,第二个 stringtwo。所以最后,我的 NSMutableArray 需要同时包含 onetwo

如何正确执行此操作?

for (NSDictionary* dict in dictArray) {
    NSString *string = ([dict valueForKey:@"string"] == [NSNull null]) ? @"" : [NSString stringWithFormat:@"%@", [dict valueForKey:@"string"]];  
}

最佳答案

我还建议您简化代码

NSMutableArray *results = [NSMutableArray array];
for (NSDictionary* dict in dictArray) {
    id value = dict[@"string"];
    [results addObject: value == [NSNull null] ? @"" : value];
}

关于iOS:如何从此 for 循环中获取每个字符串并将其添加到 NSMutableArray 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22054524/

相关文章:

iphone - 基于属性加载 View 的背景图像

ios - 自定义表格单元委托(delegate)

objective-c - UITableView 自定义单元格图像在滚动后消失。

ios - 返回按钮到 tableView

python - 获取类型错误 : unhashable type: 'list' in python dict

android - 在 React Native 中添加分享操作

ios - 如果应用程序在 iOS 11 中终止,是否会调用地理围栏事件?

c# - 为什么 IDictionary<TKey, TValue> 扩展 ICollection<KeyValuePair<TKey, TValue>>?

ios - UIImagePickerController 从 UICollectionViewCell 中的 UIImageView 呈现

python - 如何将计数器结果转换为元组列表