gpt4 book ai didi

objective-c - 持久 '' 可能无法响应 +method

转载 作者:行者123 更新时间:2023-12-03 17:32:49 24 4
gpt4 key购买 nike

我发现了很多关于这个问题的帖子,并且实际上已经成功删除了我从这些帖子中收到的一个警告,但其中一个警告仍然存在。

一些背景:我正在创建货币转换器作为学习 Objective C 的练习

我的汇率类别表示货币可以相互转换的汇率,如下所示:

Rate.h

//
// Rate.h
// Currency Converter
//
// Created by Matthew Spence on 08/06/2010.
// Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import <Cocoa/Cocoa.h>


@interface Rate : NSObject {
NSString* code;
NSString* description;
float rate;
}
- (id)init;
- (id)initTest;
- (id)initWithCode:(NSString*)code description:(NSString*)description rate:(float)rate;
@property(retain) NSString* code;
@property(retain) NSString* description;
@property(readwrite) float rate;
@end

评分.m

//
// Rate.m
// Currency Converter
//
// Created by Matthew Spence on 08/06/2010.
// Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "Rate.h"


@implementation Rate
@synthesize code;
@synthesize description;
@synthesize rate;
- (id)init {
return self;
}

- (id)initTest {
return self;
}

- (id)initWithCode:(NSString*)codeA description:(NSString*)descriptionA rate:(float)rateA {
self.code = codeA;
self.description = descriptionA;
self.rate = rateA;
[self init];
return self;
}
@end

警告发生在 ExchangeRates 中,这是一个包含费率矩阵的单例类:

汇率.m

//
// ExchangeRates.m
// Currency Converter
//
// Created by Matthew Spence on 02/06/2010.
// Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "ExchangeRates.h"
#import "SynthesizeSingleton.h"
#import "Rate.h"
#import "NSString+CVS.h"


@implementation ExchangeRates
@synthesize matrix;
synthesize_singleton(ExchangeRates);
- (id)init {

self = [super init];
self.matrix = [NSMapTable init];
if (self) {
NSError *error = nil;
NSString *matrixString = [NSString stringWithContentsOfURL:[NSURL URLWithString:@"http://rss.timegenie.com/forex.txt"] encoding:NSUTF8StringEncoding error:&error];
NSArray *matrixArray = [matrixString csvRows];
for(NSArray *rateArray in matrixArray) {
Rate *rate = [Rate initWithCode:[rateArray objectAtIndex:0] description:[rateArray objectAtIndex:1] rate:[rateArray objectAtIndex:2]];
[self.matrix setObject:rate forKey:[rateArray objectAtIndex:0]] ;
NSLog(@"my ns string = %@", [rateArray objectAtIndex:0]);
}
}

return self;
}
@end

上线:

Rate *rate = [Rate initWithCode...

警告:

'Rate' may not respond to '+initWithCode:description:rate:'

据我了解,这通常是由于未在 header 中声明方法或未导入 header ,但如您所见,我已经完成了这两件事。

最佳答案

您应该将 ObjC 实例初始化为

Rate* rate = [[Rate alloc] initWithCode:...];
// ^^^^^

关于objective-c - 持久 '<class>' 可能无法响应 +method,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3003906/

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