0

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

这是我试图做的:

[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)
}

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

4

2 回答 2

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];
            }
于 2014-01-30T16:53:59.240 回答
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];
}
于 2013-09-25T07:39:52.227 回答