Xcode: Expression is not assignable

Status
Not open for further replies.

ScottSwing

Ars Scholae Palatinae
807
I create a new object:

Code:
@interface Particle : NSObject

struct vec3 {
float x;
float y;
float z;
};
typedef struct vec3 vec3;

@property (readwrite, nonatomic) vec3 vec;

And allocate it
Code:
Particle *p = [Particle new];

p.vec.x = 0; // Error


And I get the error that the expression is not assignable. I can assign the property to another variable of that type but I can't change its components. Why is it not assignable and how do I make it assignable?
 
You are running into one of the reasons old-timers warn against using dot-notation. http://blog.bignerdranch.com/83-83/

As Mark mentions the generated code for your property looks something like this:
Code:
- (vec3)vec {
    return _vec;
}

This doesn't do what you intend, since the message is returning a copy of the object's vec3 structure. To actually manipulate it's value you need to return a pointer to the structure, and use C's member operator. We typically wouldn't do something like this in Objective-C though.

Your options are pretty much these:

1) Take a look at how one usually manipulates CGPoint within the Cocoa frameworks, and pattern the usage of your vector structure after that.
2) Add -setX:, -setY:, and -setZ: messages to the Particle object.
3) Convert vec3 into an NSObject and see if performance is adequate with the messaging overhead associated.
 
Status
Not open for further replies.