This is driving me crazy. I have a string that will either be 0 or 1 always. Whenever I try to find out what it is, my application crashes.

if([[loggedUser isStaff] isEqualToString:@"1"])
{                         
[self performSegueWithIdentifier:@"staffLogin" sender:self];
}
else{
[self performSegueWithIdentifier:@"userLogin" sender:self];
}

This is what the console says:

[740:a0b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFBoolean isEqualToString:]: unrecognized selector sent to instance 0x2d16350'

What's going on? I've checked the debugger and the string that it's checking for is appropriately displaying 0 and 1 as it should.

有帮助吗?

解决方案 2

[loggedUser isStaff] seems to be an NSNumber / Boolean object yet you are trying to compare it to an NSString. Booleans and NSNUmbers do not have an isEqualToString method.

If you want to compare whether the user isStaff, you should try:

if ([[loggedUser isStaff] boolValue])
{
[self performSegueWithIdentifier:@"staffLogin" sender:self];
}
else{
[self performSegueWithIdentifier:@"userLogin" sender:self];
}

其他提示

You're trying to call an NSString method on a boolean. You just need to check whether or not the boolean value is true.

if([[loggedUser isStaff] boolValue])
{                         
    [self performSegueWithIdentifier:@"staffLogin" sender:self];
}
else{
    [self performSegueWithIdentifier:@"userLogin" sender:self];
}

assuming isStaff is an NSNumber object holding a boolean value.

if([loggedUser isStaff])
{                         
    [self performSegueWithIdentifier:@"staffLogin" sender:self];
}
else{
    [self performSegueWithIdentifier:@"userLogin" sender:self];
}

assuming isStaff is a boolan value.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top