khadar
khadar

Reputation: 137

compare NSString and integer?

I have a string that I need to compare with an integer value.

I did this so far :

NSString *rad = [NSString stringWithFormat: @"%@", combo.selectedText];
srchParam.radius = rad;
if (srchparam.radius > 5)
{
    //code here
}

What am I doing wrong ?

Upvotes: 1

Views: 2425

Answers (6)

Fry
Fry

Reputation: 6275

Try this:

if ([combo.selectedText intValue] > 5) {
   //code here
}

Upvotes: 3

Anatoliy Gatt
Anatoliy Gatt

Reputation: 2491

Thats would be right!

if([[srchparam.radius] integerValue] > 5)

This example working in Xcode 4.5 with iOS SDK 6.0 if you still on Xcode 4.4 just change integerValue on intValue.

Upvotes: 0

Rajneesh071
Rajneesh071

Reputation: 31081

NSString *rad=[NSString stringWithFormat:@"%@",combo.selectedText];
if([rad intValue]>5)
{
 //code here
}

Upvotes: 1

Nitish
Nitish

Reputation: 14113

I guess radius is an integer here. Thus :

NSString *rad=[NSString stringWithFormat:@"%@",combo.selectedText];
srchParam.radius =[rad intValue];
if((srchparam.radius)>5)
{
 //code here
}

Upvotes: 1

user529758
user529758

Reputation:

Yes. Comparing an NSString using the < operator will compare its memory address, not magically its contents (remember: Objective-C is not C++, there's no operator overloading). You have to compare the string's numerical value to the other integer using

if (([srchparam.radius intValue]) > 5)
{
     // code here
}

Also, please use whitespaces - your code will be nicer and easier to read.

Upvotes: 1

Martol1ni
Martol1ni

Reputation: 4702

srchParam.radius = [rad intValue];

Upvotes: 1

Related Questions