views:

242

answers:

2

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.

+4  A: 

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.

Barry Wark
This answer is correct, but WRT the original code, it probably makes more sense to change the ivar not to be a pointer. It's pretty rare to have a pointer to a public struct as an instance variable.
Chuck
+2  A: 

You've got a pointer to a cpVect in your instance Variable but not in your property.

Try this:

@property (assign) cpVect * forceVector;

Mel
thanks to both of you, that worked a charm. I've spent close to 6 hours trying to get this working. I wish I could make both of you the right answer.
gargantaun