开发者

Getters / Setters / Assigning one object to another

开发者 https://www.devze.com 2023-01-09 13:53 出处:网络
I have a class called Chair. I have a view controller that contains an object of type Chair. At some point, I am trying to assign my viewcontrollers instance to another instance of a Chair object as

I have a class called Chair.

I have a view controller that contains an object of type Chair.

At some point, I am trying to assign my viewcontrollers instance to another instance of a Chair object as such:

[viewcontroller setChair: thisChair];

My setter looks as such:

- (void) setChair:(Chair *)thisEntryChair;
{
        myVCChair = thisEntryChair; 
}

I am getting this error:

[setChair:]: unrecognized selector sent to instance 

whether I use a setter function or plainly as such or use a setter:

viewcontroller.myVCChair = thisEntryChair;

This approach, if I have done it in the same manner as I have other variables which I believe I have, I assume is having 开发者_如何转开发issues as this is a custom object not an inbuilt one?

Help!


I think the class name and the field name being the same might be a problem. Try renaming your member variable aChair or something to see if it works. Then you can choose a name you like better.

UPDATE BASED ON QUESTION EDIT:

@synthesize shouldn't be used if you want your own setter. The setter should be named

 -(void) setmyVCChair: (Chair*) aChair {
     myVCChair = aChair;
 }

Then you can use viewcontroller.myVCChair = anotherChair


so in your example the code would be:

[viewcontroller setChair:somechair];

or

viewcontroller.chair = someChair;

The internal class ivar myVCChair isn't exposed.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号