持久的'<class>'可能不响应+ method </class>

时间:2010-06-09 07:44:29

标签: objective-c cocoa warnings compiler-warnings

我在这个问题上发现了很多帖子,并且实际上设法删除了我从这些中获得的警告之一,但是其中一个仍然存在。

某些背景信息:我正在创建货币转换器作为学习目标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

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

ExchangeR中的警告发生在一个包含费率矩阵的单例类中:

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:'

正如我所理解的那样,它通常是由于未在标题中声明的方法或未导入的标题,但正如您所看到的,我已经完成了两者。

1 个答案:

答案 0 :(得分:4)

您应该将ObjC实例初始化为

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