简体   繁体   English

从NSArray Objective-C创建NSMutableArray

[英]Creating a NSMutableArray from a NSArray Objective-C

I have an NSArray that gives me the following data: 我有一个NSArray,它给了我以下数据:

01/14/2013 13:28:06.559 IUser Reader [71164: c07] (
         {
         "id_acompanhante" = "";
         "id_evento" = 34;
         "user_id" = 1;
         "inserido_por" = "Himself";
         name = iUser;
         status = done;
         type = User;
     }
         {
         "id_acompanhante" = 1;
         "id_evento" = 34;
         "user_id" = 1;
         "inserido_por" = iUser;
         name = "Mayara Roca";
         status = naofeito;
         type = companion;
     }
)

How do I play this data to a NSMutableArray and add another field within each item. 如何将此数据播放到NSMutableArray并在每个项目中添加另一个字段。 example: 例:

  {
     "id_acompanhante" = "";
     "id_evento" = 34;
     "user_id" = 1;
     "inserido_por" = "Himself";
     name = IUser;
     status = done;
     type = User;
     tag = 2;
 }

I added one more field "TAG". 我又增加了一个字段“TAG”。

How do I do this? 我该怎么做呢?

NSMutableArray *mutableArray = [NSMutableArray array];

for (NSDictionary *dictionary in yourArray) {
    NSMutableDictionary *mutDictionary = [dictionary mutableCopy];

    [mutDictionary setObject:[NSNumber numberWithInt:2] forKey:@"tag"];

    [mutableArray addObject:mutDictionary];
}

This is how to do what you asked. 这是你怎么做的。 Making the array mutable will not allow you to change the contents of a dictionary inside it. 使数组可变不允许您更改其中的字典的内容。 You need to pull out each dictionary and make that mutable and then store it back in the array. 您需要提取每个字典并使其变为可变,然后将其存储回数组中。

Easy 简单

NSMutableArray *mArray = [NSMutableArray arrayWithArray:yourArray]
[mArray addObject:yourObject];

It's as easy as calling mutableCopy on your array: 它就像在你的数组上调用mutableCopy一样简单:

NSArray * myArray = @[@"one", @"two", @"three"];
NSMutableArray * myMut = [myArray mutableCopy];
NSLog(@"My Mut = %@", myMut);

Hope that helps! 希望有所帮助!

Update Here is an example that iterates over your original array, converts items to mutable dictionary and inserts the new property. 更新此处是一个迭代原始数组的示例,将项目转换为可变字典并插入新属性。 You then toss your original array and use the myMut version. 然后抛出原始数组并使用myMut版本。

NSArray * myArray = @[@{@"key1":@"value1"}, @{@"key2":@"value2"}];
NSMutableArray * myMut = [[NSMutableArray alloc] init];
for (NSDictionary * dict in myArray) {
    NSMutableDictionary * mutDict = [dict mutableCopy];
    [mutDict setObject:@"2" forKey:@"tag"]; // Add new properties
    [myMut addObject:mutDict];
}

试试这个

NSMutableArray *aMutableArray = [NSMutableArray arrayWithArray:anArray];

As far as I understand, you dont need a mutable array but you need to convert the NSDictionaries within your array to be mutable. 据我所知,你不需要一个可变数组,但你需要将数组中的NSDictionaries转换为可变的。 This is also answered by others. 其他人也回答了这个问题。

But I want to show a nifty trick how you can set add a key/value pair to all mutable Dictionaries with on line. 但我想展示一个漂亮的技巧,你可以设置如何在线添加一个键/值对所有可变字典。 I use much simpler data as you, so that it is easier to see, what happens. 我使用更简单的数据,因此更容易看到,会发生什么。

//just an example array of mutable Dictionary
NSMutableArray *array = [NSMutableArray array];
[array addObject:[NSMutableDictionary dictionaryWithObject:@"one" forKey:@(1)]];
[array addObject:[NSMutableDictionary dictionaryWithObject:@"two" forKey:@(2)]];
[array addObject:[NSMutableDictionary dictionaryWithObject:@"three" forKey:@(3)]];

// and here is the trick
[array setValue:@"10" forKey:@"tag"];

The array now contains 该数组现在包含

(
{
    tag = 10;
    1 = one;
},
{
    tag = 10;
    2 = two;
},
{
    3 = three;
    tag = 10;
}
)

from the NSArray docs 来自NSArray文档

setValue:forKey:
Invokes setValue:forKey: on each of the array's items using the specified value and key. 使用指定的值和键在每个数组的项上调用setValue:forKey :.

 - (void)setValue:(id)value forKey:(NSString *)key 
NSArray *theOldArray; // your old array
NSMutableArray *theMutableAry = [NSMutableArray array];
for (NSDictionary *dicItem in theOldArray)
{
    NSMutableDictionary *dicWithOneMoreField = [NSMutableDictionary dictionaryWithDictionary:dicItem];
    [dicWithOneMoreField setValue:@"your value for tag" forKey:@"tag"];
    [theMutableAry addObject:dicWithOneMoreField];
}

or try the other way 或者尝试另一种方式

NSData* archivedData = [NSKeyedArchiver archivedDataWithRootObject:theOldArray];
NSMutableArray *mutableArray = [NSKeyedUnarchiver unarchiveObjectWithData:archivedData];
for (NSDictionary *dicItem in mutableArray)
{
    [dicItem setValue:@"your value for tag" forKey:@"tag"];
}

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

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