persistant '<class>' may not respond to +method
I've found a bunch of posts on this problem and have actually managed to removed one of the warnings i was getting from these however one persists.
Some context: I'm creating currency converter as an exercise in learning Objective C
My Rate class represents a rate that a currency can be converted to or from looks like this:
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;
se开发者_运维技巧lf.description = descriptionA;
self.rate = rateA;
[self init];
return self;
}
@end
The warning is occurring here in ExchangeRates a singleton class that holds a matrix of rates:
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
On the line:
Rate *rate = [Rate initWithCode...
Warning:
'Rate' may not respond to '+initWithCode:description:rate:'
As I understand it its normally due the method not being declared in the header or the header not being imported, but as you can see I've done both.
You should initialize an ObjC instance as
Rate* rate = [[Rate alloc] initWithCode:...];
// ^^^^^
精彩评论