Getters/Setters/将一个对象分配给另一个对象

发布于 2024-09-11 20:58:04 字数 608 浏览 5 评论 0原文

我有一门叫做主席的课程。

我有一个视图控制器,其中包含一个椅子类型的对象。

在某些时候,我试图将我的 viewcontrollers 实例分配给 Chair 对象的另一个实例,如下所示:

[viewcontroller setChair: thisChair];

我的 setter 看起来像这样:

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

我收到此错误:

[setChair:]: unrecognized selector sent to instance 

无论我使用 setter 函数还是直接使用 setter 函数还是使用 setter:

viewcontroller.myVCChair = thisEntryChair;

这方法,如果我以与我相信我有的其他变量相同的方式完成它,我认为存在问题,因为这是一个自定义对象而不是内置对象?

帮助!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

空‖城人不在 2024-09-18 20:58:04

我认为类名和字段名相同可能会出现问题。尝试重命名您的成员变量 aChair 或其他名称,看看它是否有效。然后您可以选择一个您更喜欢的名称。

根据问题编辑更新:

如果您想要自己的设置器,则不应使用@synthesize。 setter 应该被命名

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

然后你可以使用 viewcontroller.myVCChair = anotherChair

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

浅沫记忆 2024-09-18 20:58:04

所以在您的示例中,代码将是:

[viewcontroller setChair:somechair];

viewcontroller.chair = someChair;

内部类 ivar myVCChair 未公开。

so in your example the code would be:

[viewcontroller setChair:somechair];

or

viewcontroller.chair = someChair;

The internal class ivar myVCChair isn't exposed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文