iOS, Use struct in Objective C, struct values not assignable from super view
Solution 1
Try this:
struct customStruct aStruct = object.myStruct;
aStruct.a = someValue;
object.myStruct = aStruct
It is exactly the same situation as not being able to do this:
view.frame.size.width = aWidthValue;
BTW declaring a struct inside a class interface seems like a very bad style. This is much cleaner:
typedef struct {
float a;
float b;
float c;
} customStruct;
@interface MyClass : UIView
@property (assign, nonatomic) customStruct myStruct;
Solution 2
This is because object.myStruct
returns a copy of your structure member and there is no point of changing member a
of that copy.
You should do get the entire struct change the member and then set the struct member again (using the get/set synthesized methods)
Solution 3
Define it outside/before interface
scope:
struct LevelPath{
int theme;
int level;
};
//here goes your interface line
@interface BNGameViewController : UIViewController{
}
//create propertee here
@property (nonatomic, assign) struct LevelPath levelPath;
Solution 4
To add on to the earlier suggestions, I'd suggest to not store your struct as a property, but a member variable. E.g:
@interface MyClass () {
CGPoint myPoint;
}
Then in your class you can assign new values to that struct directly:
myPoint.x = 100.0;
instead of when it's a @property, which is read-only:
myPoint = CGPointMake(100.0, myPoint.y);
Given that this isn't an NSObject subclass, unless you are overwriting the getter/setter methods, I don't see the purpose of making it an @property
anymore, since that is mostly useful for ARC to help with your objects' memory management. But please correct me if I'm wrong on that point (ouch).
Comments
-
Mr Ordinary almost 2 years
I am trying to use a struct to store three values as a unit so to speak. I am getting an error: "
Expression not assignable
" when I try to assign values to the struct's values from the object's super view.Anyone know why this is?
in my class's .h file I have defined the struct and a property
@interface MyClass : UIView { struct customStruct { float a; float b; float c; }; } @property (assign, nonatomic) struct customStruct myStruct;
from the super view I try to assign a value and I get an error: "
Expression not assignable
"object.myStruct.a = someValue;