0
-(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?

Mutch95
  • 271
  • 3
  • 14

1 Answers1

2

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.

Suhas
  • 1,500
  • 11
  • 15
  • is there any other way i convert this id type into an `sknode` without creating a variable otherwise it just defeats the purpose of me using an array for what i want to do – Mutch95 May 05 '14 at 10:20
  • I've created the variable just for readability. You can still do the following `if([_arrayOfChunks[0] isKindOfClass [SKNode class]]){[(SKNode *)_arrayOfChunks[0] setPosition:CGPointMake(screenSizeHalfW, screenSizeHalfH-100)];}` – Suhas May 05 '14 at 10:22
  • Like So? `[(SKNode*)_arrayOfChunks[0] setPosition:CGPointMake(screenSizeHalfW, screenSizeHalfH-100)];` it compiles without the if statement and still does the same thing, one other question why is `(SKNode*)` before `_arrayOfChunks[0]` dow it work like some sort of return value? if so where does it return it? – Mutch95 May 05 '14 at 10:25
  • See my question above? @Suhas – Mutch95 May 05 '14 at 10:31
  • @Mutch95 Unlike C arrays which can only store values of a particular type, NSArray can store multiple types of objects, hence it only recognises objects as type `id`. For example, _arrayOfChunks[0] can be object of type SKNode and _arrayOfChunks[1] can be an object of type NSString. So its very important that you use the if condition. Else it will result in a crash as NSString does not respond to `setPosition:`. Objective-C is a dynamically typed language, I suggeset you read more about it before you delve further. – Suhas May 05 '14 at 11:57
  • Regarding `(SKNode *)` before `_arrayOfChunks[0]` yes its works like a return value. Its similar to doing `[_arrayOfChunks objectAtIndex:0]` where `objectAtIndex` returns object of type `id` – Suhas May 05 '14 at 12:00
  • i ran it without the if statement and without the (sknode*) it still runs fine haven't had a crash yet as i know all values set in the array are sknode's should i change it back still or does it not really matter? – Mutch95 May 05 '14 at 12:34
  • @Mutch95 It's just not about getting it to run.. Its about making your source code fail safe :) You'll have to think about all possible scenarios coz if you are contributing to huge open source projects you are not the only person writing code. Not very important as of now, but better to keep it in mind.. – Suhas May 05 '14 at 14:24
  • This will help you understand `id` better http://stackoverflow.com/questions/5555855/dynamic-typing-objective-c-how-does-it-work – Suhas May 05 '14 at 14:27