iOS,在目标 C 中使用结构,结构值不能从超级视图分配
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15272052/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
iOS, Use struct in Objective C, struct values not assignable from super view
提问by Mrwolfy
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.
我正在尝试使用结构来存储三个值作为一个单位可以这么说。Expression not assignable
当我尝试从对象的超级视图为结构的值赋值时,出现错误:“ ”。
Anyone know why this is?
有谁知道这是为什么?
in my class's .h file I have defined the struct and a property
在我班级的 .h 文件中,我定义了结构和属性
@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
"
从超级视图中,我尝试分配一个值,但出现错误:“ Expression not assignable
”
object.myStruct.a = someValue;
回答by Engin Kurutepe
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;
回答by giorashc
This is because object.myStruct
returns a copy of your structure member and there is no point of changing member a
of that copy.
这是因为object.myStruct
返回结构成员的副本,并且没有必要更改a
该副本的成员。
You should do get the entire struct change the member and then set the struct member again (using the get/set synthesized methods)
您应该让整个结构体更改成员,然后再次设置结构体成员(使用 get/set 合成方法)
回答by rptwsthi
Define it outside/before interface
scope:
在interface
范围之外/之前定义它:
struct LevelPath{
int theme;
int level;
};
//here goes your interface line
@interface BNGameViewController : UIViewController{
}
//create propertee here
@property (nonatomic, assign) struct LevelPath levelPath;
回答by user959947
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:
而不是@property,它是只读的:
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).
鉴于这不是 NSObject 子类,除非您覆盖 getter/setter 方法,否则我看不到将其设置为一个的目的@property
,因为这对于 ARC 帮助您的对象的内存管理最有用。但是,如果我在这一点上错了,请纠正我(哎哟)。