将方法添加到符合NSComparisonMethods协议的类时出错

时间:2013-02-20 00:46:00

标签: objective-c cocoa

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

Fraction.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..

的main.m

#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 ..)

3 个答案:

答案 0 :(得分: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;
  
     

如果接收者小于object,则此方法应返回NSOrderedAscending,   如果接收者大于对象,则为NSOrderedDescending   如果接收者和对象相等,则为NSOrderedSame。例如,   NSString没有实现在此声明的大多数方法   非正式协议,但NSString对象仍然处理消息   正确符合此协议,因为NSString实现了   比较:满足必要要求的方法。可可也   包括适当的比较: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;
}

答案 1 :(得分:2)

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

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

  

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

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

-(BOOL)isEqualTo:(id)object
{
    if (self.someValue == object.someValue &&
        self.otherValue == object.otherValue) 
        return YES;
    else 
        return NO;
}

答案 2 :(得分: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]);
}