从 Cocoa 中的 C 方法访问属性
我正在尝试学习 Objective C 和 Objective C。 可可,但我无法访问对象内的属性。 具体来说是来自 C 方法的对象。 我正在使用花栗鼠动力学库。
Chipmunk 有一个类似于 NSPoint 的东西,称为 cpVect。 现在我在对象内定义 cpVect 没有问题,但是当我尝试使用 @property & 来创建访问器时 @synthesize我不断收到错误:所以
@interface ControlsLayer : Layer {
Sprite * touchMarker, *dragMarker;
cpVect * forceVector;
}
工作正常,
但
@interface ControlsLayer : Layer {
Sprite * touchMarker, *dragMarker;
cpVect * forceVector;
}
@property (retain) cpVect forceVector;
@end
给我错误“带有'retain'的property'forceVector'必须是对象类型”
所以如果没有'retain'我会得到一个不同的错误
"type of property 'forceVector' does not match type of ivar 'forceVector'"
我兜圈子试图弄清楚这一点,是否有我可以使用的特定类型,花栗鼠和可可之间是否不兼容,或者......或者......我不知道。 Chipmunk 的文档非常少,我发现的所有示例似乎都没有使用对象,所有示例都只使用一个类来处理所有内容。
任何帮助,非常感谢。 这件事让我发疯。
I'm trying to learn Objective C & Cocoa, but I just can't manage to access a property inside an Object. Specifically an object from a C method. I'm working with the chipmunk dynamics library.
Chipmunk has something similar to NSPoint called cpVect. Now I have no problem defining a cpVect inside my object, but when I try to make the accessors using @property & @synthesize I keep getting errors: so
@interface ControlsLayer : Layer {
Sprite * touchMarker, *dragMarker;
cpVect * forceVector;
}
works fine
but
@interface ControlsLayer : Layer {
Sprite * touchMarker, *dragMarker;
cpVect * forceVector;
}
@property (retain) cpVect forceVector;
@end
gives me the error "property 'forceVector' with 'retain' must be of object type"
so without 'retain' i get an different error
"type of property 'forceVector' does not match type of ivar 'forceVector'"
I'm going round in circles trying to figure this out, is there a particular type I can use, is it an incompatibility between chipmunk and cocoa, or... or.... I don't know. Chipmunk is very light on the documentation and all the examples I've found don't seem to use objects, all the examples just use one class to process everything.
Any help, greatly appreciated. This thing is driving me nuts.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您得到的错误是因为
retain
的语义(即引用计数内存管理,setter 增加新值的引用计数并减少旧值的引用计数)才有意义对于 Objective-C 对象。 属性的默认语义是retain
,但您可以指定该属性为assign
,如下所示:我假设该属性实际上是一个
cpVect*,而不是您编写的
cpVect
。The error your are getting is because the semantics of
retain
(ie. reference counted memory management, with the setter incrementing the ref count on the new value and decrementing the ref count of the old value) only make sense for Objective-C objects. The default semantics for properties areretain
, but you can specify that the property beassign
like so:where I assume that the property is actually a
cpVect*
, not acpVect
as you've written.您的实例变量中有一个指向 cpVect 的指针,但在您的属性中却没有。
试试这个:
@property(分配)cpVect *forceVector;
You've got a pointer to a cpVect in your instance Variable but not in your property.
Try this:
@property (assign) cpVect * forceVector;