Alison K
Alison K

Reputation: 145

Why is an NSString 'forgetting' what it is?

I was hoping for some help to understand why I need to re-cast my variable when it is a string from the start.

Here's the code:

+ (BOOL)hasOperandComponents:(NSString *)operandToTest
{
    NSArray *componentsOfOperand = [[NSString stringWithFormat:@"%@",operandToTest] componentsSeparatedByString:@" "];
    if (componentsOfOperand.count>1) return YES; return NO;
}

If I don't use the embedded call to 'stringWithFormat' then I get the rather common error:

-[__NSCFNumber componentsSeparatedByString:]: unrecognized selector sent to instance

I have been able to find answers to what this error means and hence how to avoid it (see my code above) by searching other Q's and A's... but no good explanation as to why my operandToTest seems to 'forget' that it is a NSString and become an _NSCFNumber.

I'm suspicious that it is because this is a Class method... but why would that matter when a specific instance of NSString *operandToTest is passed to the class method?

Please help?

Upvotes: 0

Views: 141

Answers (1)

Lily Ballard
Lily Ballard

Reputation: 185681

The problem is you don't have a string to begin with. The value that you're passing into the method as operandToTest is, in fact, an NSNumber*. You need to look at the calling function to figure out why this is.

Upvotes: 2

Related Questions