Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change NSString to int value?

Tags:

I am having trouble trying to run this test due to an error on the first line of this code that states:

Incompatible pointer to integer conversion initializing 'int' with an expression of type 'NSString *'

- (IBAction)Button:(id)sender  {   int x = TramNumber.text;   if (x < 9)   {      Tramresult.text = [NSString stringWithFormat:@"lol"];   }   else   {     NSLog (@"x is less than 9!");   } } @end 

Please help. I am on iOS and running xCode 5.1.1 if that helps.

like image 392
needshelp Avatar asked Oct 13 '14 09:10

needshelp


People also ask

How to convert string to integer in objective c?

6 int answer = [@"42" intValue]; 7 NSString *answerString = 8 [NSString stringWithFormat: @"%d", answer]; 9 NSNumber *boxedAnswer = 10 [NSNumber numberWithInt: answer]; 11 NSCAssert([answerString isEqualToString: 12 [boxedAnswer stringValue]], 13 @"Both strings should be the same");

What is NSString?

A static, plain-text Unicode string object that bridges to String ; use NSString when you need reference semantics or other Foundation-specific behavior.


1 Answers

You are representing NSString value wrong.

Use this code sample to solve your problem:

int x = [TramNumber.text intValue]; 

To represent int value from your textfield.

like image 160
Oleg Gordiichuk Avatar answered Sep 19 '22 15:09

Oleg Gordiichuk