Objective-C 中访问其他对象内部对象属性的正确方法

发布于 2024-10-21 02:00:05 字数 788 浏览 2 评论 0原文

我有一个单例类 SharedDataObject,它有另一个类对象 myClass。 MyClass 也有 BOOL 变量。我为 MyClass 提供了访问器(非原子、保留),并为 myClass 实例变量提供了访问器(分配、读写)。我也综合了它们中的每一个。但是,当我访问它们以使用

[[SharedDataObject getInstance].myClass available];

OR

[[SharedDataObject getInstance].myClass setAvailable:YES];

读取值或写入值时,它会给我以下警告:

 no 'available:' method found
 no 'setAvailable' method found

但我可以向其读取和写入值。我想删除警告。我做错了什么?或者访问方式是否错误?

类声明是:

@interface SharedDataObject : NSObject {
    MyClass *myClass;
}
@property (nonatomic, retain) MyClass *myClass;

+ (SharedDataObject*) getInstance;

@end


@interface MyClass : NSObject {
    BOOL available;
}
@property (assign, readwrite) BOOL available;

@end

I have a Singleton Class SharedDataObject which has a another class object myClass. MyClass has BOOL variables as well. I provided accessors (nonatomic,retain) for MyClass and (assign, readwrite) to myClass Instance variables. I have also synthesized each one of them. But when I access them to read value or write value with

[[SharedDataObject getInstance].myClass available];

OR

[[SharedDataObject getInstance].myClass setAvailable:YES];

It gives me following warning:

 no 'available:' method found
 no 'setAvailable' method found

But I can read and write values to it. I want to remove warnings. What am I doing wrong? Or if it is a wrong way to access it?

Class Declarations are:

@interface SharedDataObject : NSObject {
    MyClass *myClass;
}
@property (nonatomic, retain) MyClass *myClass;

+ (SharedDataObject*) getInstance;

@end


@interface MyClass : NSObject {
    BOOL available;
}
@property (assign, readwrite) BOOL available;

@end

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

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

发布评论

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

评论(3

呆° 2024-10-28 02:00:05

检查这一点:

  1. 包含 MyClass 单例的 myClass 属性返回 MyClass* 的 .h
  2. ,而不是某些基类(在问题更新中回答)

Check this:

  1. Include the .h for MyClass
  2. The singleton's myClass property returns MyClass*, not some base-class (answered in update to question)
我不在是我 2024-10-28 02:00:05

如果你这样做会发生什么

[SharedDataObject getInstance].myClass.available

What happens if you do

[SharedDataObject getInstance].myClass.available
带刺的爱情 2024-10-28 02:00:05

如果我很了解您,您的 myClass 是 SharedDataObject 类的类属性,因此您无法访问它,因为它是实例属性。

您能为我们提供完整的类声明吗?

有效吗

[[SharedDataObject myClass] available]

If I've understood you well, your myClass is a class property of your SharedDataObject class, so you cannot access it as it was a instance property.

Can you provide us with the full declaration of the classes?

Does

[[SharedDataObject myClass] available]

works?

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