Note that there are some explanatory texts on larger screens.

plurals
  1. POCasting in Objective-C does not work as expected
    text
    copied!<p>I am new to programming with Objective-C and Stackoverflow, and I need some help.</p> <p>I'm trying to get an object from a NSMutableArray and check one of its internal properties. I figured the best way to do this is by doing the following cast:</p> <pre><code>GenericRoom *room = (GenericRoom*)[myRooms objectAtIndex: currentIndex + side]; if (room.myType == EMPTY) { return YES; } </code></pre> <p>The problem is that when I create a class of type GenericRoom, your constructor already defines myType as EMPTY. When I insert the instances in NSMutrableArray, I changed to myType TAVERN this way:</p> <pre><code>NSMutableArray *myRooms = [[NSMutableArray alloc] initWithCapacity:15]; for (int i = 0: i &lt;15: i + +) {     GenericRoom tmpRoom * = [[GenericRoom alloc] initWithType: TAVERN];     myRooms insertObject: tmpRoom atIndex i]; } </code></pre> <p>But when I do the cast that quote up there, it simply creates a new instance of the object GenericRoom, instead of copying the object inside NSMutableArray, making its result is always YES, my failing vereficação.</p> <p>Is there any better way to solve this problem? Thank you all.</p> <p>EDIT: The complete code</p> <p>GenericRoom.h</p> <pre><code>#import &lt;Foundation/Foundation.h&gt; #import "cocos2d.h" enum Rooms { EMPTY, TAVERN, WARRIORGUILD, MAGEGUILD }roomsType; @interface GenericRoom : CCLayer { CCSprite *mySprite; enum Rooms myType; } @property (nonatomic, retain) CCSprite *mySprite; @property enum Rooms myType; @property int test; - (id)initWithSprite: (NSString *)file; - (id)initWithType: (enum Rooms)roomType; @end </code></pre> <p>GenericRoom.m</p> <pre><code>#import "GenericRoom.h" @implementation GenericRoom @synthesize mySprite, myType, test; -(id)init { if (self = [super init]) { } return self; } -(id)initWithType: (enum Rooms) roomType { if (self = [super init]) { myType = roomType; } return self; } -(id)initWithSprite: (NSString *)file { if (self = [super init]) { mySprite = [CCSprite spriteWithFile:file]; [self addChild: mySprite]; } return self; } @end </code></pre> <p>RoomManager.h</p> <pre><code>#import &lt;Foundation/Foundation.h&gt; #import "cocos2d.h" #import "GenericRoom.h" #import "RoomTavern.h" #import "RoomEmpty.h" enum Sides { LEFT, RIGHT, UP, DOWN }theSides; @interface RoomManager : CCLayer { CGSize size; NSMutableArray *myRooms; } - (void) CreateRoom: (enum Rooms)roomType; @end </code></pre> <p>RoomManager.m</p> <pre><code>@implementation RoomManager -(id)init { if (self = [super init]) { size = [[CCDirector sharedDirector] winSize]; myRooms = [[NSMutableArray alloc] initWithCapacity:15]; for (int i = 0; i &lt; 15; i++) { GenericRoom *tmpRoom = [[GenericRoom alloc] initWithType:TAVERN]; tmpRoom.test = 10; [myRooms insertObject:tmpRoom atIndex:i]; } [self CreateRoom:TAVERN]; } return self; } - (void) CreateRoom: (enum Rooms)roomType { switch (roomType) { case TAVERN: { //Create the Tavern Main Room RoomTavern *tmpRoom = [[RoomTavern alloc] initWithSprite:@"room-hd.png"]; tmpRoom.mySprite.position = ccp(size.width/2,size.height/2); [self addChild:tmpRoom]; [myRooms removeObjectAtIndex:8]; [myRooms insertObject:tmpRoom atIndex:8]; if ([self CheckAdjacentRooms:8 andSide:LEFT]) { RoomEmpty *tmpEmptyRoom = [[RoomEmpty alloc] initWithSprite:@"roomToBuild-hd.png"]; tmpEmptyRoom.mySprite.position = ccp(tmpRoom.mySprite.position.x - tmpRoom.mySprite.contentSize.width, tmpRoom.mySprite.position.y); [self addChild:tmpEmptyRoom]; [myRooms insertObject:tmpEmptyRoom atIndex:7]; } break; } default: break; } } - (BOOL) CheckAdjacentRooms: (int)currentIndex andSide:(enum Sides)side { int leftRightSide = 0; if(side == LEFT) leftRightSide = -1; else if (side == RIGHT) leftRightSide = 1; GenericRoom *roomTmp = (GenericRoom *)[myRooms objectAtIndex:currentIndex + side]; if (roomTmp.myType == EMPTY) { return YES; } else return NO; } @end </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload