0

I have a mutable array that has a range of numbers (that are changed dynamically later on if that helps), I grab a random number's index from that array and want to stick it in another array (also mutable). I'm not sure how to grab the object at a certain index and copy it.

Here's what I tried to do:

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

How can I take the object at a specific index of the first array and copy it over at the end of the second array?

3
  • possible duplicate of Is there some literal dictionary or array syntax in Objective-C? Commented Sep 24, 2013 at 14:26
  • Hm, "grab" it with objectAtIndex: and "copy" it with copy (check the documentation for details) ? Commented Sep 24, 2013 at 17:35
  • I did try that, but nxt is an id not an integer so it's incompatible to use objectAtIndex:nxt because objectAtIndex: requires an int Commented Sep 24, 2013 at 23:34

2 Answers 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];
            }
Sign up to request clarification or add additional context in comments.

Comments

0

I'm not sure how to close this, but the suggested thread had the answer I was looking for!

This was my end code if anyone is interested:

[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];
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.