添加三个或更多的数组

时间:2014-12-16 04:02:34

标签: ios objective-c nsmutablearray nsarray

我有一个nsarray的输出,如:

2014-12-16 04:55:30.345 Syndasis[17108:7559904] (
  "Hello"
)
2014-12-16 04:55:30.348 Syndasis[17108:7559904] (
"Ciao"
)
2014-12-16 04:55:30.351 Syndasis[17108:7559904] (
"Hola"
)

而且我不知道如何添加到一个NSMutableArray。如果我这样做:

NSMutableArray *ns = [[NSMutableArray alloc] init];
[ns addObject:array];
NSLog(@"%@",ns);

输出结果为:

2014-12-16 04:59:04.148 Syndasis[17180:7587364] (
    (
    "Hello"
    )
)
2014-12-16 04:59:04.150 Syndasis[17180:7587364] (
    (
    "Ciao"
    )
)

我从以下网址获取数据:

NSMutableArray *track = [[NSMutableArray alloc] init];

NSDictionary *summary = [dct objectForKey:@"tracks"];
for (NSDictionary *tracks in summary) {
     [track addObject:[tracks objectForKey:@"foreign_id"]];
}
NSArray *arrayPR = [track firstObject];

NSMutableArray *ns = [NSMutableArray arrayWithObjects:arrayPR, nil];
NSLog(@"%@",ns);

谢谢!

跟踪回复:

2014-12-16 05:25:29.895 Syndasis[17505:7776895] (
    "Hi",
    "Hola"
)
2014-12-16 05:25:29.895 Syndasis[17505:7776895] (
   "Ciao"
)

我想做:从第一个字符串中创建一个nsarray for ever object

 2014-12-16 05:25:29.896 Syndasis[17505:7776895] (
    "Hi",
    "Ciao"
  )  

5 个答案:

答案 0 :(得分:1)

根据我的理解,我不是100%清楚你的问题,我认为你在寻找:

NSMutableArray *ns = [NSMutableArray arrayWithArray: firstArray];
[ns addObjectsFromArray: secondArray];
[ns addObjectsFromArray: thirdArray]; 

答案 1 :(得分:0)

NSMutableArray *track = [[NSMutableArray alloc] init];
    NSDictionary *summary = [dct objectForKey:@"tracks"];
    for (NSDictionary *tracks in summary)
    {
//In tracks dictionary, the object for key 'foreign_id' is a array of strings.
        NSArray* lArray = [tracks objectForKey:@"foreign_id"];
//pick first object from array and add it into mutable array.
        [track addObject:[lArray firstObject]];
    }
    NSLog(@"%@",track);

答案 2 :(得分:0)

我还有一种方法可供你使用。它可能会有所帮助。

让我们在NSMutableString中添加所有值,每个值之间有一个分隔符。

然后将此字符串中的每个值分开,将为您提供数组对象。

见下面的代码:

NSMutableString *mString = [NSMutableString new];
for (NSDictionary *tracks in summary) {
    [mString appendString:[NSString stringWithFormat:@"%@,",[tracks objectForKey:@"foreign_id"]]];
}
NSMutableArray *array = [NSMutableArray arrayWithArray:[mString componentsSeparatedByString:@","]];
mString = nil;

答案 3 :(得分:0)

如果albumLists是一个可变数组的数组,并且您想要每个子数组中第一个对象的数组,则可以使用KVC Collection Operator

NSArray* firstTracks = [albumLists valueForKeyPath:@"@unionOfObjects.firstObject"];

当然,你可能有单独的数组album1&你想要这样做的album2,所以你可以这样做来制作数组数组,然后使用集合运算符:

NSArray* firstTracks = [@[album1, album2] valueForKeyPath:@"@unionOfObjects.firstObject"];

答案 4 :(得分:0)

最后我得到了解决方案!我为那些有同样问题的人提供解决方案。

我记得我过去的Javascript开发者。

NSMutableArray *nsarray = [NSMutableArray new];
NSArray *array = [[[dct objectForKey:@"tracks"] firstObject] objectForKey:@"foreign_id"];
[nsarray addObject:array];

int len = nsarray.count;
NSMutableArray *var = [[NSMutableArray alloc] init];
for (int i = 0; i < len; i++) {
    [var addObject:nsarray[i]];
}
NSLog(@"solution %@",var);

感谢您的帮助!