0

I have a chunk of code that dose not seem to work because of expected expression error.

NSMutableArray *worldData =[[NSMutableArray alloc] initWithArray:@[

                        @[ @1, @2, @3, @4,@5,@6],
                        @[ @1, @2, @3, @4,@5,@6],
                        @[ @1, @2, @3, @4,@5,@6],
                        @[ @1, @2, @3, @4,@5,@6],

                        ]];
int *x = 1;

if ( int 1 == (worldData objectAtIndex:1)){

    UIImage *block31 = [UIImage imageNamed:
                      @"grass2.png"];
}

}

3 Answers 3

1

I don't know what this is really supposed to be:

int *x = 1;

if ( int 1 == (worldData objectAtIndex:1)){

I think you probably mean:

if ([[[worldData objectAtIndex:1] objectAtIndex:1] intValue] == 1)

or more concisely, you can use subscripts and literals:

if ([worldData[1][1]  isEqual: @1])
Sign up to request clarification or add additional context in comments.

1 Comment

You're right, I did't realise direct comparison of a number literal has undefined behaviour.
1

you don't need to declare the int inside the if block. Just rewrite as:

if ( 1 == [[[worldData objectAtIndex:1] objectAtIndex:1] intValue]){

1 Comment

Love fixing one problem only to notice that isn't the only problem.
0
int *x = 1;

Pointers are created to store address of variable.

It should be something like this:

int num = 1;
int *x = #

Another issue:

if ( int 1 == (worldData objectAtIndex:1)){ 

The above is meaningless.

As you are comparing an NSNumber value to int. Either typecast one to NSNumber or another to intValue.

if ( [@1 isEqual: worldData[1]]) //[worldData objectAtIndex:1]

or

if (1 == [worldData[1] intValue])

You are trying to access by [worldData objectAtIndex:] the complete array. So you cant compare them with an integer. Either you need another array or you need to go into deep by again going to 2nd dimension of array.

[[worldData objectAtIndex:] objectAtIndex:]

or,

worldData[row][col]

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.