属性表达式的Objective-C地址
我需要访问地址的财产,但有问题。示例代码是属性表达式的Objective-C地址,objective-c,objective-c-runtime,objective-c-2.0,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 {
@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是请求的属性表达式的地址正如注释所示,您不能获取属性的地址。属性实际上只是一种承诺,即所讨论的对象为某些值提供访问器。该值本身可能存在,也可能不存在于实例变量中。例如,名为
fullName
的属性的getter可以通过连接firstName
和lastName
属性的值来动态生成所需的值
由于需要将SDL\u Rect
的地址传递到SDL\u BlitSurface()
,因此可以先将必要的属性复制到局部变量中,然后传递该变量的地址:
Rectangle *rect = [[Rectangle alloc] init];
SDL_Rect wall = rect.wall;
SDL_BlitSurface(camera, NULL, background, &wall);
如果需要在调用SDL\u BlitSurface()
后保留留在wall
中的值,请在调用后再次复制该值:
rect.wall = wall;
请求的物业地址表示:
@preperty (nonatomic,copy) NSString *name;
如果您想获取self.name
的地址。您不能这样编写代码:
NSLog (@"%p",&(self.name));
- (NSString *)name {
return _name;
}
因为实际上,self.name
是getter方法,如下所示:
NSLog (@"%p",&(self.name));
- (NSString *)name {
return _name;
}
因此您无法获取方法的地址。我遇到过类似的情况,子类需要访问父类中定义的CGAffineTransform。答案来自@orpheist对这个问题的回答:。它确实涉及到向矩形类添加一个方法
@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
现在您可以使用addressOfWall,例如:
- (void)testWall
{
Rectangle *rect = [Rectangle standardRectangle];
XCTAssertEqual(100, [rect addressOfWall]->size.height);
}
不能,它是一个属性。SDL_BlitSurface(摄影机,NULL,背景,);-最后一个需要的参数是CGRect,而不仅仅是x值。请参考:这是有意义的,但奇怪的是,它是不允许的,因为它在技术上是可计算的。我想知道是什么模糊的原因导致他们禁止这样做(假设有一个有效的理由)