0

我需要通过添加几个符合 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..)

4

3 回答 3

3
if (selfCopy == object) 

这仅检查指针是否相同

尝试

-(BOOL) isEqualTo:(id)object
{
    return self.numerator == object.numerator && self.denumerator == object.denumerator :
}

好的,这太简单了,[fracA setTo:1 over:4]应该[fracA setTo:2 over:8]是相等的,但你应该明白这一点。


NSObject 为其中许多方法提供的默认实现适用于实现单个比较方法的对象,其选择器、签名和描述与以下内容匹配:

- (NSComparisonResult)compare:(id)object;

如果接收者小于对象,则此方法应返回 NSOrderedAscending,如果接收者大于对象,则返回 NSOrderedDescending,如果接收者和对象相等,则返回 NSOrderedSame。例如,NSString 并没有实现这个非正式协议中声明的大部分方法,但是 NSString 对象仍然可以正确处理符合这个协议的消息,因为 NSString 实现了一个满足必要要求的 compare: 方法。Cocoa 还包括适当的 compare: NSDate、NSDecimalNumber 和 NSValue 类的方法实现。

所以你最好点个-compare:

-(NSOrderResult) compare:(Fraction *)otherFraction
{
    Fraction *selfReduced = [self fractionReduced]; // should return a unretaind reduced copy
    Fraction *otherReduced= [otherFraction fractionReduced];
    float selfAbs = (float)selfReduced.nominator / (float)selfReduced.denominator;
    float otherAbs = (float)selfReduced.nominator / (float)selfReduced.denominator;
    if (selfAbs > otherAbs) return NSOrderedAscending;
    if (selfAbs > otherAbs) return NSOrderedDescending;
    return NSOrderedSame;
}
于 2013-02-20T00:50:46.993 回答
2

if (selfCopy == object)将始终为假,因为您正在比较指针。

您必须对您的实例进行适当的比较。

我不能像这样测试对象(selfCopy == object)吗?我应该做 selfCopy.numerator

不,您不需要self. 此外,您甚至没有创建 的副本self,您只是分配了一个新对象。这就是它应该的样子。

-(BOOL)isEqualTo:(id)object
{
    if (self.someValue == object.someValue &&
        self.otherValue == object.otherValue) 
        return YES;
    else 
        return NO;
}
于 2013-02-20T00:50:28.170 回答
0

我的版本:

-(BOOL) isEqualTo: (Fraction *) f
{
Fraction *temp1 = [[Fraction alloc] init];
Fraction *temp2 = [[Fraction alloc] init];
[temp1 setTo:[self numerator] over:[self denominator]];
[temp2 setTo:[f numerator] over:[f denominator]];
[temp1 reduce];
[temp2 reduce];

return ([temp1 numerator] == [temp2 numerator])
        && ([temp1 denominator] == [temp2 denominator]);
}
于 2013-11-07T23:43:22.707 回答