Eric Brotto
Eric Brotto

Reputation: 54251

Trying to remove a decimal from NSString

This is bizarre.

Developing for the iPhone I have an NSString:

 distanceFromTargetString = @"6178266.000000"
 // this is not actually code, but a value taken from elsewhere. It is a proper NSString though. 

When I use this

NSArray *listItems = [distanceFromTargetString componentsSeparatedByString:@"."];
distanceFromTargetString = [listItems objectAtIndex:0];

or this

 [distanceFromTarget setText: distanceFromTargetString];

I get something like this

-[NSDecimalNumber isEqualToString:]: unrecognized selector sent to instance 0x1cac40
2011-07-21 14:29:24.226 AssassinBeta[7230:707] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSDecimalNumber isEqualToString:]: unrecognized selector sent to instance 0x1cac40'

Any ideas?

Upvotes: 0

Views: 2121

Answers (4)

Monolo
Monolo

Reputation: 18253

You could try to set a breakpoint at the line

[distanceFromTarget setText: distanceFromTargetString];

and see if distanceFromTargetString is actually an NSString.

As mentioned above, somehow an NSNumber has sneaked in somewhere.

Upvotes: 0

user756245
user756245

Reputation:

You could try :

NSInteger i = [distanceFromTargetString integerValue];
NSString s = [NSString stringWithFormat:@"%d", i];
// you got your string

Upvotes: 1

Rob Napier
Rob Napier

Reputation: 299385

At some point you are assigning an NSDecimalNumber to distanceFromTargetString rather than an NSString. There is no run-time type checking of assignments in Objective C, so this is totally "legal":

NSDecimalNumber *number = [NSDecimalNumber ....];
[array addObject:number];
NSString *string = [array lastObject];

The above will generate no errors or warnings until you try to send NSString methods to string, at which point you will get an exception (crash) like you show above.

Do an audit of everywhere you assign distanceFromTargetString, and everywhere you use NSDecimalNumber. Somewhere you're crossing the streams.

Upvotes: 2

dasdom
dasdom

Reputation: 14073

You are somewhere calling isEqualToString with a NSDecimalNumber as the receiver. What is distanceFromTarget? Is this an NSDecimalNumber?

The first thing should work.

Upvotes: 1

Related Questions