Send copy of class to view class so it can render him? ( iPhone )
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 t开发者_StackOverflowype
:/
Help? I'm pretty new at this, heh.
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.
精彩评论