Objective-C problems

Member
Posts: 43
Joined: 2009.02
Post: #1
I'm working on an Interactive Fiction game, hopefully to become something like the old Wizardry Gold, as a first project. I'm getting some wierd (atleast to me) errors tho.

Code:
- (IBAction)createNorth:(id)sender {
    IFRoom* centerRoom = [IFRoom init];
    IFRoom* northernRoom = [IFRoom init];
    
    centerRoom.northRoom = northernRoom;
    northernRoom.southRoom = centerRoom;
    
    centerRoom.roomName = @"The Central Room";
    northernRoom.roomName = @"The Northern Room";
    currentRoom = centerRoom;
}

That's the button that causes the problem

vvv IFRoom.h vvv
Code:
#import <Cocoa/Cocoa.h>


@interface IFRoom : NSObject {
    NSString* roomName;
    NSString* description;
    IFRoom* westRoom;
    IFRoom* eastRoom;
    IFRoom* northRoom;
    IFRoom* southRoom;
}
@property (retain) NSString* roomName;
@property (retain) NSString* description;
@property (retain) IFRoom* westRoom;
@property (retain) IFRoom* eastRoom;
@property (retain) IFRoom* northRoom;
@property (retain) IFRoom* southRoom;

@end

And now IFRoom.m vvv
Code:
#import "IFRoom.h"


@implementation IFRoom

@synthesize roomName;
@synthesize description;
@synthesize westRoom;
@synthesize eastRoom;
@synthesize northRoom;
@synthesize southRoom;

- (id) init {
    if ( self = [super init] ) {
        self.roomName = @"Generic Room";
        self.description = @"This is a generic room.";
    }
    return self;
}

@end

The error doesn't stop the app, it just keeps the createNorth button from working properly. I still get the 'You walk north/east/west/south.' fine, but not the other parts.

I get it two errors at a time (two errors per click):
2009-04-24 09:21:47.251 graphicalIF[12764:10b] *** +[IFRoom setNorthRoom:]: unrecognized selector sent to class 0x4064
2009-04-24 09:21:47.252 graphicalIF[12764:10b] *** +[IFRoom setNorthRoom:]: unrecognized selector sent to class 0x4064

with some rudimentary tests I've found the problem is in the button createNorth, on the line centerRoom.northRoom = northernRoom;
If I comment that out, I get the same sort of error for the x.southRoom = x;

Any help?
Quote this message in a reply
Moderator
Posts: 623
Joined: 2007.09
Post: #2
TimMcD Wrote:
Code:
- (IBAction)createNorth:(id)sender {
    IFRoom* centerRoom = [IFRoom init];
    IFRoom* northernRoom = [IFRoom init];
    
    centerRoom.northRoom = northernRoom;
    northernRoom.southRoom = centerRoom;
    
    centerRoom.roomName = @"The Central Room";
    northernRoom.roomName = @"The Northern Room";
    currentRoom = centerRoom;
}

That's the button that causes the problem

vvv IFRoom.h vvv
Code:
#import <Cocoa/Cocoa.h>


@interface IFRoom : NSObject {
    NSString* roomName;
    NSString* description;
    IFRoom* westRoom;
    IFRoom* eastRoom;
    IFRoom* northRoom;
    IFRoom* southRoom;
}
@property (retain) NSString* roomName;
@property (retain) NSString* description;
@property (retain) IFRoom* westRoom;
@property (retain) IFRoom* eastRoom;
@property (retain) IFRoom* northRoom;
@property (retain) IFRoom* southRoom;

@end

The error doesn't stop the app, it just keeps the createNorth button from working properly. I still get the 'You walk north/east/west/south.' fine, but not the other parts.

I get it two errors at a time (two errors per click):
2009-04-24 09:21:47.251 graphicalIF[12764:10b] *** +[IFRoom setNorthRoom:]: unrecognized selector sent to class 0x4064
2009-04-24 09:21:47.252 graphicalIF[12764:10b] *** +[IFRoom setNorthRoom:]: unrecognized selector sent to class 0x4064

with some rudimentary tests I've found the problem is in the button createNorth, on the line centerRoom.northRoom = northernRoom;
If I comment that out, I get the same sort of error for the x.southRoom = x;

Any help?

Basically, IFRoom is receiving a message(setNorthRoom: in this case) that it doesn't recognize.

Firstly, I would suggest adding the readwrite tag to your @property - I think it's the default, but you may as well add it.

Secondly, I believe createNorth: should be changed to this:

Code:
- (IBAction)createNorth:(id)sender {
    IFRoom* centerRoom = [[IFRoom alloc] init];
    IFRoom* northernRoom = [[IFRoom alloc] init];
    
    centerRoom.northRoom = northernRoom;
    northernRoom.southRoom = centerRoom;
    
    centerRoom.roomName = @"The Central Room";
    northernRoom.roomName = @"The Northern Room";
    currentRoom = centerRoom;
}

If I am not mistaken, you are calling the instance method -init on the IFRoom class. What you need to do is call +alloc, a global class method that returns an allocated instance, and then call the -init instance method.

It is very important to keep your class methods (+) separate from your instance methods(-).

- Lincoln Green
http://www.binkworks.com/
Quote this message in a reply
Member
Posts: 43
Joined: 2009.02
Post: #3
Heh, I was thinking that since I was on Leopard (Y'know, nifty Garbage collection Smile, that I didn't have to alloc. Thanks!
Quote this message in a reply
Member
Posts: 227
Joined: 2008.08
Post: #4
Garbage Collection means not having to deallocate memory manually, you still have to alloc in order for GC to be relevant.
Quote this message in a reply
Post Reply