views:

47

answers:

2

I'm making a game for the iPhone, and I have a class called Robot.

Then I have a class called View, which renders everything.

I want to send a copy of my Robot, which I defined in my ViewController, and I send it to gameView (which is View *gameView), like this:

robot = [Robot new];
[gameView setRobot: [robot copy]];

I tried to make a copy but that didn't work, I could also do it with a pointer to Robot (&robot) but sometimes it just crashes ?

I tried this in my View.h @interface definition:

@property (copy) Robot* robot;

but I get the error

/RobotsAdventure/Classes/View.h:24: error: setter '-robot' argument type does not match property type

:/

Help? I'm pretty new at this, heh.

A: 

Please show your setRobot: method. Looks like it's signature differs from a @property.

kovpas
- (void) setRobot: (id)ref { robot = ref;}
Johannes Jensen
should be `- (void) setRobot: (Robot *)ref { robot = ref; } `But I suggest to remove this method completely and add `@synthesize robot;` to your implementation.You can read about properties here: http://developer.apple.com/mac/library/documentation/cocoa/Conceptual/ObjectiveC/Articles/ocProperties.html
kovpas
yeah that works but it's not what I'm aiming for...I want a pointer to the Robot, but when I do that it just crashes D:
Johannes Jensen
You already have a pointer to the robot. If that's all you want, why are you making a copy of the robot itself?
Chuck
Never mind.. I guess I'm just confused, heh. xD;
Johannes Jensen
A: 

In order to copy an object, its class has to implement the NSCopying Protocol.

See Memory Management Programming Guide for Cocoa

The simplest way to accomplish what you want is:

View.h

#import "Robot.h"
@property(nonatomic,retain) Robot *robot.

Then set it like so in the view controller:

// myView is of class View.h
myView.robot=self.robot;

By the way, naming a class "View" is simply asking for a naming collision at some point. Worse, you'll never remember what the class does months down the road when you come back to it. Neither will you be able to search for it. Instead use a highly descriptive name e.g. RobotDisplayView_MyProjectName. With autocomplete it doesn't take any longer to type out than a short name.

TechZen
ah, thanks :] !
Johannes Jensen