Objective-C Address of property expression
Asked Answered
H

3

9

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

Hunsinger answered 14/12, 2014 at 10:56 Comment(3)
You cannot, it is a property.Manger
SDL_BlitSurface(camera, NULL, background, <it should be CGRect here>); - last parameter which is required is a CGRect, not only x value.Franck
Refer: wiki.libsdl.org/SDL_BlitSurfaceFranck
H
11

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.

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);

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;
Halfprice answered 16/12, 2014 at 16:32 Comment(0)
N
2

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.

@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:

- (void)testWall
{
    Rectangle *rect = [Rectangle standardRectangle];
    XCTAssertEqual(100, [rect addressOfWall]->size.height);
}
Nodular answered 25/10, 2016 at 19:8 Comment(0)
V
1

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:

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

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

- (NSString *)name {
    return _name;
}

so you cannot get address of method.

Ventail answered 2/7, 2016 at 8:48 Comment(1)
This makes sense, but it's still odd that it's not allowed, given that it is technically computable. I wonder what obscure reason lead to them prohibiting this (assuming there is a valid reason)Sanctuary

© 2022 - 2024 — McMap. All rights reserved.