Objective C: Function help
Greetings,
I have the following function:
-(NSString*) nudgePhoneNumber:(NSString*)num{
NSString *nudged=@"";
[nudged stringByReplacingOccurrencesOfString:@"+" withString:@""];
[nudged stringByReplacingOccurrencesOfString:@"\\s" withString:@""];
[nudged stringByReplacingOccurrencesOfString:@"-" withString:@""];
[nudged stringByReplacingOccurrencesOfString:@"." withString:@""];
[nudged stringByReplacingOccurrencesOfString:@"/" withString:@""];
//NSLog(nudged);
return nudged;
}
And I'm calling it as follows:
-(IBAction) phoneNumberUp:(id)sender{
NSString *mob=phoneNumber.text;
NSString *result=@"";
[result nudgePhoneNumber:mob];
...
}
But I keep getting an exception: "-[NSCFException nudgePhoneNumber:]: unrecognized selector sent to instance" and "Terminating app due to uncaught exception 'NSInvalidArgumentException'"
I'm quite new to Objective-C and think I just need someone to have a quick look开发者_如何学JAVA-over.
Many thanks in advance,
In which class is your method -(NSString*) nudgePhoneNumber:(NSString*)num
being declared and implemented?
You're creating an NSString object on the line NSString *result=@"";
and then you're trying to call the method using the NSString object. As NSString doesn't have a method called nudgePhoneNumber:
you're getting an unrecognized selector runtime error (this exception is thrown when a message is sent to an object that doesn't respond to that message (key terminology there).
I suggest you take another look at how you "call methods" (really you're "sending a message") in Objective-C.
if both nudgePhoneNumber:
and phoneNumberUp:
are defined in the same class, then phoneNumberUp:
should be more like this:
-(IBAction) phoneNumberUp:(id)sender{
NSString *mob=phoneNumber.text;
NSString *result=nil;
result = [self nudgePhoneNumber:mob];
...
}
also, I think the nudgePhoneNumber:
method isn't really doing anything. You might have to do it more like this:
-(NSString*) nudgePhoneNumber:(NSString*)num{
NSString *nudged = num;
nudged = [nudged stringByReplacingOccurrencesOfString:@"+" withString:@""];
nudged = [nudged stringByReplacingOccurrencesOfString:@"\\s" withString:@""];
nudged = [nudged stringByReplacingOccurrencesOfString:@"-" withString:@""];
nudged = [nudged stringByReplacingOccurrencesOfString:@"." withString:@""];
nudged = [nudged stringByReplacingOccurrencesOfString:@"/" withString:@""];
//NSLog(nudged);
return nudged;
}
精彩评论