Ios CGPoint作为属性
出于许多原因,我一直试图避免使用IVAR,而是使用属性 如果我设置为属性变量,例如Ios CGPoint作为属性,ios,xcode,properties,Ios,Xcode,Properties,出于许多原因,我一直试图避免使用IVAR,而是使用属性 如果我设置为属性变量,例如CGPointtype: @interface Foo () { @property (nonatomic, assign) CGPoint lastPoint; } 我得到: Expression is not assignable 我知道,CGRect,CGPoint等都是一种结构,这就是它不起作用的原因。 当然,我可以用\u lastPoint来解决这个问题 我假设我必须创建自定义设置器?但是也
CGPoint
type:
@interface Foo () {
@property (nonatomic, assign) CGPoint lastPoint;
}
我得到:
Expression is not assignable
我知道,CGRect
,CGPoint
等都是一种结构,这就是它不起作用的原因。
当然,我可以用\u lastPoint
来解决这个问题
我假设我必须创建自定义设置器?但是也许这是不可能的或多余的
我想要一些类似于:
self.lastPoin.x = 10;
您不能这样分配
self.lastPoint.x=10代码>但您可以指定likeself.lastPoint=CGPointMake(10,0)代码>。因为setter方法仅适用于lastPoint
,而不适用于lastPoint.x
。因为你已经知道,lastPoint是一个结构。如果你想喜欢这个赋值,你可以比如变量
@interface Foo () {
CGPoint lastPoint;
}
使用属性时,使用的是setter方法。CGPoint有两个ivar,比如x和y。它们未声明为属性
因此,您可以直接设置点,因为您已经为其创建了属性,也可以通过直接访问点直接为x和y赋值。您在使用self.lastPoint.x=10时遇到问题代码>
记住,这与[self lastPoint]相同代码>
您需要执行以下操作:
CGPoint point = self.lastPoint;
point.x = 10;
self.lastPoint = point;
只需尝试作为@property(非原子)CGPoint lastPoint;没有意义…添加@interface Foo(){@property(非原子,赋值)CGPoint lastPoint;}是不可能的。@Natarajan是的。为什么不呢?@rmaddy是否允许objectiveC在大括号内设置“@property”?