gpt4 book ai didi

objective-c - 向符合 NSComparisonMethods 协议(protocol)的类添加方法时出错

转载 作者:行者123 更新时间:2023-12-03 17:30:18 31 4
gpt4 key购买 nike

我需要通过添加几个符合 NSComparisonMethods 协议(protocol)的方法来扩展 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..

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

最佳答案

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]可能应该是相等的,但你应该明白这一点。

<小时/>

The default implementation provided for many of these methods by NSObject is appropriate for objects that implement a single comparison method whose selector, signature, and description match the following:

- (NSComparisonResult)compare:(id)object;

如果接收者小于对象,此方法应返回 NSOrderedAscending, NSOrderedDescending 如果接收者大于对象,并且 NSOrderedSame 如果接收者和对象相等。例如, NSString 没有实现这里声明的大部分方法 非正式协议(protocol),但 NSString 对象仍然处理消息 正确遵守此协议(protocol),因为 NSString 实现了 比较:满足必要要求的方法。 cocoa 也 包括适当的比较: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;
}

关于objective-c - 向符合 NSComparisonMethods 协议(protocol)的类添加方法时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14970395/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com