Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS, Use struct in Objective C, struct values not assignable from super view

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;
like image 302
Mr Ordinary Avatar asked Mar 07 '13 13:03

Mr Ordinary


People also ask

What is the difference between array and structure in Objective-C?

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,...

What is object OBJECT structure in Objective C?

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.

Why is Objective-C still used for iOS development?

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.

How to fix viewcontroller cannot mix Objective-C with C++ in Xcode?

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.


2 Answers

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;
like image 117
Engin Kurutepe Avatar answered Oct 11 '22 17:10

Engin Kurutepe


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)

like image 34
giorashc Avatar answered Oct 11 '22 18:10

giorashc