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
@property offers a way to define the information that a class is intended to encapsulate. If you declare an object/variable using @property, then that object/variable will be accessible to other classes importing its class.
In Objective-C, message expressions are enclosed in brackets: [receiver message] The receiver is an object, and the message tells it what to do. In source code, the message is simply the name of a method and any parameters that are passed to it.
Objective-C is the primary programming language you use when writing software for OS X and iOS. It's a superset of the C programming language and provides object-oriented capabilities and a dynamic runtime.
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;
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);
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With