开发者

Properties / Property: getter doesn't work! Objective-C iPhone

in my programm, I made a property:

@property (nonatomic, retain) IBOutlet UIImageView *block;
@synthesize block;

-

now if开发者_JAVA技巧 I do:

NSLog(@"BLOCK = %i", block.center.y);

It just says: BLOCK = 0

but my block UIImageView object never is on y = 0!

please help!


CGPoint.y is CGFloat, so you need to use %f to print it out.


A property and an instance variable are different things; a property represents state exposed by your class, while an instance variable is one way you can implement state for your class.

When you wrote block.center.y you were accessing the instance variable named block, not invoking the property getter. To invoke the property getter you must always use either dot or message syntax, such as:

CGFloat centerY;

centerY = self.block.center.y;  // sends -block getter to self
centerY = [self block].center.y;  // sends -block getter to self

Here's an example where all of these differ: isEnabled_ is the instance variable, enabled is the property, and -isEnabled is the getter method invoked by the property:

@interface View : NSObject {
@private
    BOOL isEnabled_;
}
@property (getter=isEnabled) BOOL enabled;
@end

@implementation View
@synthesize enabled = isEnabled_;
@end

The getter=isEnabled attribute tells the compiler to generate -isEnabled messages when getting the enabled property. The @synthesize defines the enabled property as being backed by the instance variable isEnabled_.

You can therefore access the property this way:

BOOL shouldDrawView;

shouldDrawView = someView.enabled;  // sends -isEnabled to someView
shouldDrawView = [someView isEnabled];  // also sends -isEnabled to someView
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜