简体   繁体   中英

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.

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.

If you want to edit the caption for example for the dictionary at position 0:

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

But you will have to create them as NSMutableDictionary then.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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