属性表达式的Objective-C地址。

时间:2022-12-28 22:32:49

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 is Address of property expression requested

矩形。x是请求的属性表达式的地址

3 个解决方案

#1


6  

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.

正如评论所指出的,你不能取物业的地址。属性实际上只是一个承诺,该对象为某些值提供了访问器。值本身可能甚至可能不存在于实例变量中。例如,名为fullName的属性的getter可能通过连接firstName和lastName属性的值动态生成所需的值。

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:

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

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

If you need to preserve the value left in wall after the call to SDL_BlitSurface(), copy it back again after the call:

如果您需要在调用SDL_BlitSurface()之后保留在wall中的值,则在调用之后再次复制它:

rect.wall = wall;

#2


0  

I had a similar situation with subclasses needing to access a CGAffineTransform defined in the parent class. The answer came from @orpheist's answer to this question: Get the address of an Objective-c property (which is a C struct). It does involve adding a method to your Rectangle class.

我遇到过类似的情况,子类需要访问父类中定义的CGAffineTransform。答案来自@orpheist对这个问题的回答:获取Objective-c属性的地址(C struct)。它确实需要向您的矩形类添加一个方法。

@interface Rectangle : NSObject
{
    NSRect wall;
    NSRect ground;
}
@property NSRect wall;
@property NSRect ground;
@end

@implementation Rectangle
@synthesize wall = _wall; //x;
@synthesize ground = _ground; //y;

- (const NSRect *) addressOfWall {
    return &_wall;
}

- (const NSRect *) addressOfGround {
    return &_ground;
}

+(instancetype)standardRectangle
{
    Rectangle *newInstance = [[self alloc] init];
    newInstance.wall = NSMakeRect(0,0, 300, 100);
    newInstance.ground = NSMakeRect(0 ,0, 300, 450);
    return newInstance;
}
@end

Now you can use, for instance, addressOfWall thus:

现在你可以使用,例如,addressOfWall:

- (void)testWall
{
    Rectangle *rect = [Rectangle standardRectangle];
    XCTAssertEqual(100, [rect addressOfWall]->size.height);
}

#3


0  

Address of property expression requested that means:

请求的财产表达地址:

@preperty (nonatomic,copy) NSString *name;

if you want to get the address of self.name. You cannot write the code like this:

如果你想要得到self。name的地址。您不能这样编写代码:

NSLog (@"%p",&(self.name));

Because in fact,self.name is getter method, like this:

因为事实上,self.name getter方法,像这样:

- (NSString *)name {
    return _name;
}

so you cannot get address of method.

所以你不能得到方法的地址。

#1


6  

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.

正如评论所指出的,你不能取物业的地址。属性实际上只是一个承诺,该对象为某些值提供了访问器。值本身可能甚至可能不存在于实例变量中。例如,名为fullName的属性的getter可能通过连接firstName和lastName属性的值动态生成所需的值。

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:

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

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

If you need to preserve the value left in wall after the call to SDL_BlitSurface(), copy it back again after the call:

如果您需要在调用SDL_BlitSurface()之后保留在wall中的值,则在调用之后再次复制它:

rect.wall = wall;

#2


0  

I had a similar situation with subclasses needing to access a CGAffineTransform defined in the parent class. The answer came from @orpheist's answer to this question: Get the address of an Objective-c property (which is a C struct). It does involve adding a method to your Rectangle class.

我遇到过类似的情况,子类需要访问父类中定义的CGAffineTransform。答案来自@orpheist对这个问题的回答:获取Objective-c属性的地址(C struct)。它确实需要向您的矩形类添加一个方法。

@interface Rectangle : NSObject
{
    NSRect wall;
    NSRect ground;
}
@property NSRect wall;
@property NSRect ground;
@end

@implementation Rectangle
@synthesize wall = _wall; //x;
@synthesize ground = _ground; //y;

- (const NSRect *) addressOfWall {
    return &_wall;
}

- (const NSRect *) addressOfGround {
    return &_ground;
}

+(instancetype)standardRectangle
{
    Rectangle *newInstance = [[self alloc] init];
    newInstance.wall = NSMakeRect(0,0, 300, 100);
    newInstance.ground = NSMakeRect(0 ,0, 300, 450);
    return newInstance;
}
@end

Now you can use, for instance, addressOfWall thus:

现在你可以使用,例如,addressOfWall:

- (void)testWall
{
    Rectangle *rect = [Rectangle standardRectangle];
    XCTAssertEqual(100, [rect addressOfWall]->size.height);
}

#3


0  

Address of property expression requested that means:

请求的财产表达地址:

@preperty (nonatomic,copy) NSString *name;

if you want to get the address of self.name. You cannot write the code like this:

如果你想要得到self。name的地址。您不能这样编写代码:

NSLog (@"%p",&(self.name));

Because in fact,self.name is getter method, like this:

因为事实上,self.name getter方法,像这样:

- (NSString *)name {
    return _name;
}

so you cannot get address of method.

所以你不能得到方法的地址。