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;
Objective-C arrays allow you to define type of variables that can hold several data items of the same kind but structure is another user-defined data type available in Objective-C programming which allows you to combine data items of different kinds. Structures are used to represent a record,...
Objective-C Structures. Objective-C arrays allow you to define type of variables that can hold several data items of the same kind but structure is another user-defined data type available in Objective-C programming which allows you to combine data items of different kinds.
Despite the quick rise of Swift, Objective-C is still the dominant language on iOS because of the sheer number of existing apps and libraries already created with it. One reason to use Objective-C is to port an existing C/C++ program written for another platform to iOS.
To fix this issue, rename ViewController.m to ViewController.mm. This simple naming convention tells Xcode that ViewController wants to mix Objective-C with C++. After renaming the file, the error should disappear. Let’s make the app more interactive by adding a button.
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;
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With