Question

I have a project that returns a distance as an NSString. I want to check that distance to see if it is less or equal to, or more than 10,000 feet. I am running into an error that says "Implicit conversion of 'int' to 'NSString *' is disallowed with ARC." Does anyone know how to convert the NSString into an Integer? or how to construct the code? Thank you!

- (IBAction)btnPress:(id)sender {

NSString *distanceInFeet = [[NSUserDefaults standardUserDefaults]
                           stringForKey:@"distanceInFeet"];

if ([distanceInFeet intValue] <= 10000)
{

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Close" message:@"Distance is close" delegate:self cancelButtonTitle:@"Dismiss" otherButtonTitles:nil, nil];
    [alert show];
    return;

else
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Far" message:@"Distance is far" delegate:self cancelButtonTitle:@"Dismiss" otherButtonTitles:nil, nil];
    [alert show];
    return;
}

} 
Was it helpful?

Solution

Why don't you simply try this one?

 

if ([distanceInFeet intValue]<=10000)
{

OR

if ([distanceInFeet integerValue]<=10000)
{

EDIT:

As per your edit of code, and correct error shown...

NSString *distanceInFeet = [NSString alloc] initWithFormat:@"%ld", [[NSUserDefaults standardUserDefaults]
                           stringForKey:@"distanceInFeet"]];

OTHER TIPS

Use NSNumberFormatter, it will return nil if the string is not a valid number.

NSString *distanceInFeet ...
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
[numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
NSNumber *distanceInFeetNumber = [numberFormatter numberFromString:ditanceInFeet];

if ([distanceInFeetNumber intValue] <= 10000) {
    //do stuff in here
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top