3

I want to make an array of integers with as little code as possible and pass that array to an objective C method.

I tried the below. sequence starts out as an array and is passed to setLights: but when sequence is looked at in the method (via breakpoint) it is no longer an array.

*EDIT: I didnt want to use an NSArray because an NSArray of integers is so verbose:

Using NSArray:

NSArray *sequence = [[NSArray alloc] initWithObjects: [NSNumber numberWithInt:0], [NSNumber numberWithInt:1], [NSNumber numberWithInt:2], [NSNumber numberWithInt:3], [NSNumber numberWithInt:4],[NSNumber numberWithInt:5],nil];

Using C array:

int sequence[6] = {0,1,2,3,4,5};

What am I doing wrong?

- (IBAction)testLights:(id)sender {
    int sequence[6] = {0,1,2,3,4,5};
    //int *sequence[0][1][2][3][4][5]; //also tried this
    [self setLights:sequence];
}


- (void)setLights:(int *)sequence {

    UIImageView *light=[lgtArray objectAtIndex: sequence[0]];
    light.alpha = 0;
    [UIView animateWithDuration:0.3f
                     animations:^{
                         light.alpha = 1;
                     }completion:nil
     ];


}

enter image description here enter image description here

8
  • does it help to know that it doesn't change anything that it is an Objective C method... that all Objective C functions are really C functions? Commented May 2, 2014 at 4:42
  • 2
    is there any reason NOT to use NSArray here? Commented May 2, 2014 at 4:43
  • 1
    Lookup "array decays to pointer" - This must have been answered somewhere ... Commented May 2, 2014 at 4:44
  • 3
    You can use short notation too just in case if you plan to use nsarray as NSArray *sequence = @[@(0),@(1),@(2),@(3),@(4),@(5)]; Commented May 2, 2014 at 4:58
  • 1
    No, just use NSNumbers, which is what the shorthand notation that Janak commented on is. Commented May 2, 2014 at 5:02

3 Answers 3

2

Use this syntax to pass the array:

- (void)setLights:(int[] )sequence
Sign up to request clarification or add additional context in comments.

4 Comments

Some of the other comments and answers show a better way to do what I need, but this answered my original question best
@DelightedD0D For what it's worth, I agree with the other guys that you should try to avoid C arrays. If the only reason is verbosity, then you should still use NSArray. If the reason is because a third party library uses this, then maybe you would have to use C arrays.
Agreed, I ended up going with `NSMutableArray *sequence = [@[@0, @1, @2, @3, @4, @5] mutableCopy]; for my actual project.
@Delighted0D Good decision.
1

You are running into a bizarre feature of C that has propagated through its variants: the [mostly] equivalence of pointers and arrays.

if you do

int *sequence ; 

then you can do

sequence [4] ;

or

*(sequence + 4)

Arrays and points are mostly interchangeable. Arrays in C variants are merely data allocation. Your definition of

- (void)setLights:(int *)sequence 

conveys no information array information. You can still access sequence as though it is an array. setLights simply has no intrinsic information as to how many elements sequence has allocated to it.

The problem here is that your usage of the array in setLights needs to match how you have allotted the data.

If you did

sequence [100] = 10 ;

it would be syntactically correct but likely to create an error.

1 Comment

Change *(sequence + 4] to *(sequence + 4)
1

Is verbose only if you want. Use literals instead:

NSArray *sequence = @[@0, @1, @2, @3, @4, @5];

And access to the value like this:

UIImageView *light = lgtArray[[sequence[0] intValue]]];

2 Comments

Can this be done with NSMutableArray? or should I then just do NSMutableArray *newSequence = [(NSArray*) sequence mutableCopy];
Yes, but make a mutable copy before: NSMutableArray *sequence = [@[@0, @1, @2, @3, @4, @5] mutableCopy];

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.