Bool method to check user input

0

Hi everybody i am trying to make a boolean method that will return value depending upon the user input for the field when submit button is clicked.

    - (BOOL)checkuser:(NSString*)input
{
    bool result = NO;


    if ([input isEqualToString:@"test"]) 
    {
        result = YES;
    }

    else
    {
        result = NO;
    }

    return result;

}

And i am refrencing this method from my program as

else if ([Username checkuser] == YES)
    { // do something
}

with following warning 'NSString' may not respond to '-checkuser'

and i am not getting why my program crashing at this point with following error.

2011-03-22 22:57:25.942 Assignment 2[824:207] -[NSCFString checkuser]: unrecognized selector sent to instance 0x4b7c050 2011-03-22 22:57:25.944 Assignment 2[824:207] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSCFString checkuser]: unrecognized selector sent to instance 0x4b7c050' * Call stack at first throw: ( 0 CoreFoundation 0x00db8be9 exceptionPreprocess + 185 1 libobjc.A.dylib 0x00f0d5c2 objc_exception_throw + 47 2 CoreFoundation 0x00dba6fb -[NSObject(NSObject) doesNotRecognizeSelector:] + 187 3 CoreFoundation 0x00d2a366 __forwarding + 966 4 CoreFoundation 0x00d29f22 _CF_forwarding_prep_0 + 50 5 Assignment 2 0x00002958 -[Assignment_2ViewController SubmitbuttonPressed:] + 727 6 UIKit 0x002c1a6e -[UIApplication sendAction:to:from:forEvent:] + 119 7 UIKit 0x003501b5 -[UIControl sendAction:to:forEvent:] + 67 8 UIKit 0x00352647 -[UIControl(Internal) _sendActionsForEvents:withEvent:] + 527 9 UIKit 0x003511f4 -[UIControl touchesEnded:withEvent:] + 458 10 UIKit 0x002e60d1 -[UIWindow _sendTouchesForEvent:] + 567 11 UIKit 0x002c737a -[UIApplication sendEvent:] + 447 12 UIKit 0x002cc732 _UIApplicationHandleEvent + 7576 13 GraphicsServices 0x016eea36 PurpleEventCallback + 1550 14 CoreFoundation 0x00d9a064 CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION + 52 15 CoreFoundation 0x00cfa6f7 __CFRunLoopDoSource1 + 215 16 CoreFoundation 0x00cf7983 __CFRunLoopRun + 979 17 CoreFoundation 0x00cf7240 CFRunLoopRunSpecific + 208 18 CoreFoundation 0x00cf7161 CFRunLoopRunInMode + 97 19 GraphicsServices 0x016ed268 GSEventRunModal + 217 20 GraphicsServices 0x016ed32d GSEventRun + 115 21 UIKit 0x002d042e UIApplicationMain + 1160 22 Assignment 2 0x00001c04 main + 102 23 Assignment 2 0x00001b95 start + 53 ) terminate called after throwing an instance of 'NSException' Program received signal: “SIGABRT”. (gdb)

nsexception
asked on Stack Overflow Mar 23, 2011 by Amrit

2 Answers

1

You're over-complicating a very simple problem:

- (BOOL) checkuser:(NSString*)input {
    return [input isEqualToString:@"test"];
}

and then to call it:

if ([Username checkUser:@"DONT FORGET YOUR PARAM HERE"]) {
    // Do something...
}

In your example, you're not passing any parameter to your method... That's a misstake hence your error.

Also, if you create a method returning a 'BOOL', make sure you're returning a 'BOOL' and not a 'bool'. You have to always keep in mind that the underlying types can be different, even if a bool can be evaluated to a BOOL.

answered on Stack Overflow Mar 23, 2011 by Doodloo
0
 else if ([Username checkuser] == YES)

checkuser is supposed to receive a NSString*, which you are not sending though and is the reason for error messages. So, it should be -

Assuming someNSString is initialized and is of type NSString*.

else if( ([Username checkuser:someNSString] ) == YES )
                           // ^^^^^^^^^^^^ Needs to be passed as the method prototype mentions so.
{
     // ....
}

NSString* someNSString = @"Forgot to pass this" ;
else if( ([Username checkuser:someNSString] ) == YES )
                           // ^^^^^^^^^^^^ Needs to be passed as the method prototype mentions so.
{
     // ....
}
answered on Stack Overflow Mar 23, 2011 by Mahesh • edited Mar 23, 2011 by Mahesh

User contributions licensed under CC BY-SA 3.0