从 Cocoa 中的 C 方法访问属性

发布于 2024-07-16 06:44:01 字数 911 浏览 3 评论 0原文

我正在尝试学习 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 技术交流群。

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

发布评论

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

评论(2

纸短情长 2024-07-23 06:44:01

您得到的错误是因为 retain 的语义(即引用计数内存管理,setter 增加新值的引用计数并减少旧值的引用计数)才有意义对于 Objective-C 对象。 属性的默认语义是 retain,但您可以指定该属性为 assign,如下所示:

@property (assign) cpVect *forceVector;

我假设该属性实际上是一个 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 are retain, but you can specify that the property be assign like so:

@property (assign) cpVect *forceVector;

where I assume that the property is actually a cpVect*, not a cpVect as you've written.

岛歌少女 2024-07-23 06:44:01

您的实例变量中有一个指向 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;

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