簡體   English   中英

從NSArray Objective-C創建NSMutableArray

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

我有一個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;
     }
)

如何將此數據播放到NSMutableArray並在每個項目中添加另一個字段。 例:

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

我又增加了一個字段“TAG”。

我該怎么做呢?

NSMutableArray *mutableArray = [NSMutableArray array];

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

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

    [mutableArray addObject:mutDictionary];
}

這是你怎么做的。 使數組可變不允許您更改其中的字典的內容。 您需要提取每個字典並使其變為可變,然后將其存儲回數組中。

簡單

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

它就像在你的數組上調用mutableCopy一樣簡單:

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

希望有所幫助!

更新此處是一個迭代原始數組的示例,將項目轉換為可變字典並插入新屬性。 然后拋出原始數組並使用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];

據我所知,你不需要一個可變數組,但你需要將數組中的NSDictionaries轉換為可變的。 其他人也回答了這個問題。

但我想展示一個漂亮的技巧,你可以設置如何在線添加一個鍵/值對所有可變字典。 我使用更簡單的數據,因此更容易看到,會發生什么。

//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"];

該數組現在包含

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

來自NSArray文檔

setValue:forKey:
使用指定的值和鍵在每個數組的項上調用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];
}

或者嘗試另一種方式

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