我需要通过添加几个符合 NSComparisonMethods 协议的方法来扩展 Fraction 类。
这是我的代码:Fraction.h
#import <Foundation/Foundation.h>
@protocol NSComparisonMethods
-(BOOL) isEqualTo:(id)object;
-(BOOL) isLessThanOrEqualTo:(id)object;
-(BOOL) isLessThan:(id)object;
-(BOOL) isGreaterThan:(id)object;
-(BOOL) isGreaterThanOrEqualTo:(id)object;
-(BOOL) isNotEqualTo:(id)object;
@end
@interface Fraction : NSObject <NSComparisonMethods>
@property int numerator, denumerator;
-(void) print: (BOOL) test;
-(void) setTo: (int) n over: (int) d;
-(double) convertToNum;
-(void) reduce;
-(id) addFrac:(id)f;
-(void) justPrint;
-(id) initWith:(int)n over:(int)f;
+(Fraction *) allocF;
+(int) count;
+(int) gCounter;
@end
分数.m
#import "Fraction.h"
@implementation Fraction
@synthesize numerator, denumerator;
-(BOOL) isEqualTo:(id)object
{
Fraction *selfCopy = [[Fraction alloc] init];
if (selfCopy == object)
return YES;
else
return NO;
}
//more methods..
主文件
#import <Foundation/Foundation.h>
#import "Fraction.h"
#import "Fraction+MathOperations.h"
int main (int argc, const char * argv[]) {
@autoreleasepool {
Fraction *fracA = [[Fraction alloc] init];
Fraction *fracB = [[Fraction alloc] init];
id Object;
[fracA setTo:1 over:4];
[fracB setTo:1 over:4];
Object = fracB;
NSLog(@"%s",[fracA isEqualTo: Object]? "Yes":"No");
}
return 0;
}
不应该正常工作吗?我No
不知道什么..是方法实现中的问题吗?我不能像这样测试对象(selfCopy == object)
吗?我应该做selfCopy.numerator
(exedra..)