Obj-C的新成员;报名参加课堂自学。我有一个练习的以下工作代码,但现在必须删除@synthesize并修改程序以处理编译器给实例变量赋予的新名称。尝试了很多事情;由于不知道@synthesize如何完全实现以及删除它意味着什么,总是会遇到编译器错误。谢谢你的任何解释,SO。
的main.m
#import "Fraction.h"
int main(int argc, const char * argv[])
{
@autoreleasepool {
Fraction *aFraction = [[Fraction alloc] init];
[aFraction setTo: 100 over: 200];
[aFraction print];
[aFraction setTo: 1 over: 3];
[aFraction print];
}
return 0;
}
Fraction.h
#import <Foundation/Foundation.h>
@interface Fraction : NSObject
@property int numerator, denominator;
-(void) print;
-(void) setTo: (int) n over: (int) d;
-(double) convertToNum;
@end
Fraction.m
#import "Fraction.h"
@implementation Fraction
@synthesize numerator, denominator;
-(void) print {
NSLog(@"%i/%i", numerator, denominator);
}
-(double) convertToNum {
if (denominator != 0) return (double) numerator / denominator;
else return NAN;
}
-(void) setTo: (int) n over: (int) d {
numerator = n;
denominator = d;
}
@end