Objective-C属性表达式的地址 [英] Objective-C Address of property expression
问题描述
我需要财产的访问地址,但是有问题.示例代码是
I need access address of property but have problem. example code is
@interface Rectangle : NSObject
{
SDL_Rect wall;
SDL_Rect ground;
}
@property SDL_Rect wall;
@property SDL_Rect ground;
@end
@implementation Rectangle
@synthesize x;
@synthesize y;
@end
@interface Graphics : NSObject
{
int w;
int h;
}
-(void) drawSurface
@end
@implementation Graphics
-(void) drawSurface
{
Rectangle *rect = [[Rectangle alloc] init];
SDL_BlitSurface(camera, NULL, background, &rect.wall);
}
@end
& rect.x是请求的属性表达式的地址
&rect.x is Address of property expression requested
推荐答案
如注释所示,您不能使用属性的地址.属性实际上只是一个承诺,即所讨论的对象为某些值提供访问器.该值本身可能存在也可能不存在于实例变量中.例如,名为fullName
的属性的吸气剂可能会通过将firstName
和lastName
属性的值进行串联来即时生成所需的值.
As the comments suggest, you cannot take the address of a property. A property is really just a promise that the object in question provides accessors for some value. The value itself may or may not even exist in an instance variable. For example, the getter for a property called fullName
might generate the required value on the fly by concatenating the values of firstName
and lastName
properties.
由于您需要将SDL_Rect
的地址传递到SDL_BlitSurface()
,因此您可以先将必要的属性复制到局部变量中,然后再传递该变量的地址:
Since you need to pass the address of a SDL_Rect
into SDL_BlitSurface()
, you could first copy the necessary property into a local variable, and then pass the address of that variable:
Rectangle *rect = [[Rectangle alloc] init];
SDL_Rect wall = rect.wall;
SDL_BlitSurface(camera, NULL, background, &wall);
如果您需要保留对SDL_BlitSurface()
的调用后留在wall
中的值,请在调用之后再次将其复制回:
If you need to preserve the value left in wall
after the call to SDL_BlitSurface()
, copy it back again after the call:
rect.wall = wall;
这篇关于Objective-C属性表达式的地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!