Objective-C 中访问其他对象内部对象属性的正确方法
我有一个单例类 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
检查这一点:
MyClass
单例的 myClass 属性返回MyClass*
的 .hCheck this:
MyClass
The singleton's myClass property returns(answered in update to question)MyClass*
, not some base-class如果你这样做会发生什么
What happens if you do
如果我很了解您,您的 myClass 是 SharedDataObject 类的类属性,因此您无法访问它,因为它是实例属性。
您能为我们提供完整的类声明吗?
有效吗
?
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
works?