简体   繁体   English

从数组创建字典

[英]creating a dictionary from array

I have an array like this: 我有一个像这样的数组:

(
    "ALAsset - Type:Photo, URLs:assets-library://asset/asset.JPG?id=EF015EF6-FC98-4ACF-9092-AF7E0196A760&ext=JPG",
    "ALAsset - Type:Photo, URLs:assets-library://asset/asset.JPG?id=6FC0C2DC-69BB-4FAD-9709-63E03182BEE1&ext=JPG",
    "ALAsset - Type:Photo, URLs:assets-library://asset/asset.JPG?id=324E4377-0BCD-431C-8A57-535BC0FC44EB&ext=JPG"
)

And am having a hard time figuring out how to create a dictionary/mutable dictionary that will have a key of "urlRep" and also "caption" for each object in the array. 并且很难弄清楚如何创建字典/可变字典,该字典/可变字典将为数组中的每个对象提供键“ urlRep”和“标题”。

So I want my dictionary to look like this: 所以我希望我的字典看起来像这样:

(
{
urlRep = 
    "ALAsset - Type:Photo, URLs:assets-library://asset/asset.JPG?id=EF015EF6-FC98-4ACF-9092-AF7E0196A760&ext=JPG",
caption = ""
},

{
urlRep = 
    "ALAsset - Type:Photo, URLs:assets-library://asset/asset.JPG?id=6FC0C2DC-69BB-4FAD-9709-63E03182BEE1&ext=JPG",
caption = ""
}

)

sorry if my formatting is off but that is the basic idea of what Im going for but am failing to achieve 抱歉,如果我的格式化功能已关闭,但这是我要做什么但无法实现的基本思想

This should do the trick: 这应该可以解决问题:

NSMutableArray *otherArray = [NSMutableArray new];
for (NSString *string in myArray)
{
    [otherArray addObject:@{@"urlRep":string, @"caption":@""}];
}

You are creating another array with a nsdictionary on every entry. 您将在每个条目上创建一个带有nsdictionary的数组。

If you want to edit the caption for example for the dictionary at position 0: 如果要编辑位置0的字典的标题,例如:

[[otherArray objectAtIndex:0] setObject:@"myValue" forKey:@"caption"];

But you will have to create them as NSMutableDictionary then. 但是您必须将它们创建为NSMutableDictionary。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM