将可变数组的特定索引处的对象复制到另一个数组的末尾

时间:2013-09-24 14:23:19

标签: objective-c nsmutablearray foundation

我有一个可变数组,其中包含一系列数字(如果有帮助,将在以后动态更改),我从该数组中获取一个随机数的索引,并希望将其粘贴到另一个数组中(也是可变的)。我不确定如何抓住某个索引处的对象并复制它。

这是我试图做的事情:

[btnRange addObject:@"12"];
[btnRange addObject:@"13"];
[btnRange addObject:@"14"];
[btnRange addObject:@"17"];
[btnRange addObject:@"18"];
[btnRange addObject:@"19"];
//start randomising and adding to btnOrder Array

for (NSInteger i=0; i <= 5; i++) {
     id nxt = btnRange[arc4random_uniform([btnRange count])];
     [btnOrder addObject:(@"%@", nxt];
     //[btnOrder addObject[btnRange(nxt)]; --didn't work
     //[btnOrder addObjectsFromArray:(btnRange. nxt]; --didn't work
     //[btnOrder addObject:nxt]; --didn't work (I'm pretty new to this)
}

如何将对象放在第一个数组的特定索引处并将其复制到第二个数组的末尾?

2 个答案:

答案 0 :(得分:1)

You can get the object from NSMutableArray using [arrayName objectAtIndex:index] 
and add object in NSMutableArray using [arrayName addObejct:object]
[arrayName objectAtIndex:index] return object

[btnRange addObject:@"12"];
            [btnRange addObject:@"13"];
            [btnRange addObject:@"14"];
            [btnRange addObject:@"17"];
            [btnRange addObject:@"18"];
            [btnRange addObject:@"19"];
            //start randomising and adding to btnOrder Array

            for (NSInteger i=0; i <= 5; i++) {
                NSString *nxt = [btnRange objectAtIndex:arc4random()%[btnRange count]];
                [btnOrder addObject:nxt];
            }

答案 1 :(得分:0)

我不确定如何关闭它,但建议的帖子有我想要的答案!

如果有人有兴趣,这是我的最终代码:

[btnRange addObject:@"12"];
[btnRange addObject:@"13"];
[btnRange addObject:@"14"];
[btnRange addObject:@"17"];
[btnRange addObject:@"18"];
[btnRange addObject:@"19"];
//start ordering
for (NSInteger i=0; i <= 5; i++) {
    id nxt = btnRange[arc4random_uniform([btnRange count])];
    btnOrder[i] = nxt;
    [btnRange removeObject:nxt];
}