6

いわば、構造体を使用して 3 つの値を 1 つの単位として格納しようとしています。Expression not assignableオブジェクトのスーパー ビューから構造体の値に値を割り当てようとすると、" " というエラーが発生します。

これがなぜなのか知っている人はいますか?

クラスの .h ファイルで、構造体とプロパティを定義しました

@interface MyClass : UIView

{
    struct customStruct {
        float a;
        float b;
        float c;
    };

}

@property (assign, nonatomic) struct customStruct myStruct;

スーパー ビューから値を割り当てようとすると、次のエラーが表示されます: " Expression not assignable"

object.myStruct.a = someValue;
4

4 に答える 4

20

これを試して:

struct customStruct aStruct = object.myStruct;
aStruct.a = someValue;
object.myStruct = aStruct

これができないのとまったく同じ状況です。

view.frame.size.width = aWidthValue;

ところで、クラス インターフェイス内で構造体を宣言することは、非常に悪いスタイルのようです。これはもっときれいです:

typedef struct { 
    float a;
    float b;
    float c;
} customStruct;

@interface MyClass : UIView

@property (assign, nonatomic) customStruct myStruct;
于 2013-03-07T13:13:23.423 に答える
6

これはobject.myStruct、構造体メンバーのコピーを返し、そのコピーのメンバーを変更する意味がないためaです。

構造体全体を取得してメンバーを変更し、構造体メンバーを再度設定する必要があります (get/set 合成メソッドを使用)。

于 2013-03-07T13:12:28.950 に答える
2

interfaceスコープの外/前に定義します。

struct LevelPath{
    int theme;
    int level;
};

//here goes your interface line
@interface BNGameViewController : UIViewController{
}
//create propertee here
@property (nonatomic, assign) struct LevelPath levelPath;
于 2013-03-07T13:12:51.490 に答える
1

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

于 2014-02-06T18:05:39.060 に答える