当CGRect是实例变量时,如何获得对CGRect元素的赋值访问 [英] How to gain assignment access to CGRect elements when the CGRect is an instance variable

查看:107
本文介绍了当CGRect是实例变量时,如何获得对CGRect元素的赋值访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

@interface some_class : some_parent {

 }
@property (nonatomic, assign) CGRect the_rect;

@end

...

@implementation some_class

@synthesize the_rect;

@end

创建some_class的实例后:

After creating an instance of some_class:

instance_of_some_class.the_rect.origin.x = 0.0;

这会产生表达式无法分配错误。

Which generates an "Expression not assignable" error.

当然这很好用:

instance_of_some_class.the_rect = CGRectMake(0,0,0,0);

我想问题是自动神奇创建的setter只知道将CGRect分配给the_rect 。好的,我明白了。我可以告诉它不要创建一个setter,这样我就可以直接访问成员变量并为struct成员分配而不必分配整个CGRect吗?

I guess the problem is that the auto-magically created setter only knows about assigning a CGRect to "the_rect". OK, I get that. Can I tell it to not create a setter so that I can access the member variable directly and make assignments to the struct members without having to assign the entire CGRect?

我想我可以将它分解为四个单独的CGFloats for origin.x,origin.y,size.width和size.height ...但你认为有一种方法可以用CGRects来做到这一点。

I suppose that I could break this out into four individual CGFloats for origin.x, origin.y, size.width and size.height...but you'd think there would be a way to do this with CGRects instead.

令人困惑的是,这当然可以正常工作:

What's confusing is that this, of course, works fine:

CGRect test = instance_of_some_class.the_rect.origin.x;

换句话说,getter知道如何导航结构并提取其中一个的值元素所以我可以使用它。相反的情况似乎并非如此,因为你无法进入一个恰好是结构的ivar,并且在不必分配整个结构的情况下对其中一个元素进行赋值。

In other words, the getter knows how to navigate the struct and pull out the value of one of its elements so I can use it. The opposite does not seem to be the case as you just can't reach into an ivar that happens to be a struct and make an assignment to one of its elements without having to assign the entire struct.

我也试过这个:

@interface some_class : some_parent {

@public

    CGRect the_rect;

 }
@property (nonatomic, assign) CGRect the_rect;

@end

在另一个模块中实例化之后:

After instantiating in another module:

instance.the_rect->origin.x = some_value;

...并且错误地说被引用的成员不是指针。我尝试使用instance.the_rect的地址......但这也不起作用。

...and got an error saying that the member being referenced is not a pointer. I tried taking the address of instance.the_rect ... but that didn't work either.

编辑为CLARIFY:这是关于创建一个有一些ivars的类碰巧是结构。然后,在其他地方实例化类后,您希望能够直接对struct ivar元素进行赋值:

EDIT TO CLARIFY: This is about creating a class that has some ivars that happen to be structs. Then, after instantiating the class somewhere else, you want to be able to make assignments to the struct ivar elements directly:

class_instance.struct_ivar.element = something;

我使用CGRect作为一个众所周知的结构的一个方便的例子,可能想要用作ivar。

I am using CGRect as a convenient example of a well-known struct that one might want to use as an ivar.

推荐答案

您通常不直接从课外访问成员变量。如果确实想要,您可以将其声明为 @public 。我想你可能要为每个rect的成员创建访问器:

You don't generally access member variables directly from outside the class. You can declare it as @public if you really want to. I think you may have to create accessors for each of the rect's members:

- (void)setTheRectX:(CGFloat)newX {
    the_rect.origin.x = newX;
}

- (void)setTheRectY:(CGFloat)newY {
    the_rect.origin.y = newY;
}

- (void)setTheRectOrigin:(CGPoint)newOrigin {
    the_rect.origin = newOrigin;
}

依此类推。

问题是当你要求实例的 the_rect 时,你没有得到指向它的指针实例具有的rect,就像你将ivar作为一个对象指针一样 - 你得到一个结构的新副本。

The issue is that when you ask for the instance's the_rect, you don't get a pointer to the same rect that the instance has, like you would if the ivar was an object pointer -- you get a new copy of the struct.

有几个问题浮在SO周围讨论这个问题: 1 2 3

There are a few questions floating around SO that discuss this issue: 1 2 3

公共伊娃:

@interface RectHolder : NSObject {

@public
    CGRect the_rect;


}

访问:

RectHolder * myRH = [[RectHolder alloc] init];
myRH->the_rect = CGRectMake(0.0, 0.0, 100, 100);

myRH->the_rect.origin.x = 10;

NSLog(@"%@", NSStringFromRect(myRH->the_rect));

这篇关于当CGRect是实例变量时,如何获得对CGRect元素的赋值访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆