当我为我的类覆盖 getter 和 setter 并在miles
其他地方设置属性时,不会调用 setter 方法。我究竟做错了什么?
我打电话给miles
这里的物业
// "entry" is an object that has a property of type CYDistance
if ([unitLabel.text isEqualToString:@"Miles"]) {
entry.distance.miles = s.floatValue;
}
else if ([unitLabel.text isEqualToString:@"Kilometers"]) {
entry.distance.kilometers = s.floatValue;
}
NSLog(@"Miles: %.2f", entry.distance.miles);
NSLog(@"Kilometers: %.2f", entry.distance.kilometers);
距离.h
#import <Foundation/Foundation.h>
@interface CYDistance : NSObject {
float miles, kilometers;
}
@property float miles;
@property float kilometers;
@end
距离.m
#import "Distance.h"
@implementation CYDistance
- (float)miles {
return miles;
}
- (float)kilometers {
return kilometers;
}
- (void)setMiles:(float)m {
miles = m;
kilometers = m * 1.609344;
NSLog(@"Set miles");
}
- (void)setKilometers:(float)k {
kilometers = k;
miles = k / 1.609344;
NSLog(@"Set kilometers");
}
@end