Objective-C属性表达式的地址

unk*_*now 6 objective-c objective-c-runtime objective-c-2.0

我需要属性的访问地址但有问题.示例代码是

@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
Run Code Online (Sandbox Code Playgroud)

&rect.x是请求的属性表达式的地址

Cal*_*leb 9

如评论所示,您无法获取房产的地址.属性实际上只是一个承诺,有问题的对象为某些值提供了访问器.值本身可能甚至不存在于实例变量中.例如,调用的属性的getter fullName可以通过连接firstNamelastName属性的值来动态生成所需的值.

由于您需要传入一个SDL_Rectinto 的地址SDL_BlitSurface(),您可以先将必要的属性复制到一个局部变量中,然后传递该变量的地址:

Rectangle *rect = [[Rectangle alloc] init];
SDL_Rect wall = rect.wall;
SDL_BlitSurface(camera, NULL, background, &wall);
Run Code Online (Sandbox Code Playgroud)

如果您需要保留wall呼叫后留下的值,请在呼叫后再SDL_BlitSurface()将其复制回来:

rect.wall = wall;
Run Code Online (Sandbox Code Playgroud)