Question

-(NSArray*)createArrayOfChunks{

    ProceduralMapGeneration *procedure = [[ProceduralMapGeneration alloc]init];
    [procedure initArrays];

    NSMutableArray *arrayMutable = [[NSMutableArray alloc] init];

    SKNode *object;

    for (int i = 0; i <= 19; i++) {
        object = [procedure createChunk];

        [arrayMutable addObject:object];
    }

    NSArray *array = [arrayMutable copy];

    return array;

}

if I create an array like so and then call the method here in another method

_arrayOfChunks = [self createArrayOfChunks];

but now I want to edit _arrayOfChunks[0].position

when I attempt to do this

_arrayOfChunks[0].position = CGPointMake(screenSizeHalfW, screenSizeHalfH-100);

I get an error

Property 'position' not found on object of type 'id'

now as you saw before when i used createArrayOfChunks: it made and instance variable object which is a pointer to SKNode so why is it telling me it has no property called position? how do i fix this?

Was it helpful?

Solution

When you get an object at a particular index from NSArray it always returns and object of type id which means that it does not include any class related information in it.

Try the following code

id object = _arrayOfChunks[0]

if([object isKindOfClass [SKNode class]])
{
   [(SKNode *)object setPosition:CGPointMake(screenSizeHalfW, screenSizeHalfH-100)];
}

This should silence the error.

We do a check to make sure that the object accessed is actually of type SKNode just make sure we do not get an exception if in future objects of other type are added to the array.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top