How do I call this method in objective-C (iOS)?
I have spent literally hours and hours trying to find the answer to this. I have tried [what I feel anyway!] to be everything. And I cannot call this method in my code with out getting this error:
Receiver type NSString for instance message does not declare a method with selector
returnAsDoubleDigits:
I was trying to refactor my code so I could reuse this method to return single digit numbers (converted to NSString
) as double digit strings (i.e. 4
becomes 04
).
Please can anyone help me here? Have I even declared it correctly etc.? Thank you so much! :)
Call something like:
[doubleDigitString returnAs开发者_如何学JAVADoubleDigits: singleDigitString];
Header file:
- (NSString *)returnAsDoubleDigits: (NSString *)digits;
Implementation file:
- (NSString *)returnAsDoubleDigits: (NSString *)digits {
if (digits.length == 1) return [NSString stringWithFormat: @"0%@", digits];
else return [NSString stringWithFormat: @"0%@", digits];
}
Where are you calling it from? in the same class? Is this what you intended?
NSString* doubleDigitString = [self returnAsDoubleDigits:digits];
You could always use categories too.
@implementation NSString (mycategory)
-(NSString *) returnAsDoubleDigits
{
NSString* doubleDigits = nil;
if (self.length == 1) {
doubleDigits = [NSString stringWithFormat: @"0%@",
self];
} else {
doubleDigits = [NSString stringWithFormat: @"0%@",
self];
}
return doubleDigits;
}
@end
and then use
NSString* doubleDigitString = [singleDigitString returnAsDoubleDigits];
The problem is that you are calling the method on an object of type NSString
, when you've declared the method in whatever class you actually put it in. You can solve this two ways: with a category on NSString, or by directing your message to an object of the type that your method is declared in.
Here's how to do a category:
In NSString+doubleDigit.h
:
@interface NSString (doubleDigit)
-(NSString*)doubleDigit;
@end
In NSString+doubleDigit.m
@implementation NSString (doubleDigit)
-(NSString*)doubleDigit {
return [NSString stringWithFormat:@"%02i", [self integerValue]];
}
@end
Then in files where you want to make this conversion, #include "NSString+doubleDigit.h"
and simply write:
NSString *doubleDigitString = [singleDigitString doubleDigit];
From what you have here, it looks like you haven't added the method to the class which doubleDigitString
belongs to. Check out Objective-C Categories to add to the existing implementation of a class.
You are seeing this the wrong way, you are attempting to call that method on a NSString class type but that method is not declared for NSStrings.
I assume you've declared that method in your own class. If you are using it in the same class as the implementation, you can do something like:
NSString *doubleDigitString = [self returnAsDoubleDigits:singleDigitString];
'self' refers to the class of your implementation. To have it work with NSStrings like you've shown above, you'd need to use a category but I'm not going to go into that because it'll confuse things further for something simple like what you are trying to achieve.
I think you need to revise the basics of Objective C before you delve in if it's taken you hours to get something simple like this working.
From what you have here, it looks like you haven't added the method to the class which doubleDigitString belongs to. Check out Objective-C Categories to add to the existing implementation of a class.
You have not declared the categories correctly.
NSString *singleDigitString = [doubleDigitString returnAsDoubleDigits];
NSString+DoubleDigit.h file
@interface NSString (doubleDigit)
- (NSString*) returnAsDoubleDigits;
@end
NSString+DoubleDigit.m file
@implementation NSString (doubleDigit)
- (NSString*) returnAsDoubleDigits {
NSString* doubleDigits = nil;
if ([self length] == 1) {
doubleDigits = [NSString stringWithFormat: @"0%@",self];
} else {
doubleDigits = [NSString stringWithFormat: @"%@", self];
}
return doubleDigits;
}
@end
精彩评论