24
votes

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.

4
I suggest you use NSInteger and not int. int 32 bit and NSInteger a typedef for the systems native bit size, so 64 on 63 bits CPU en 32 on 32 bits systems. - rckoenes
Trivially reproducible: int x = (NSString*)nil; So why does the compiler warn about that? - user2864740

4 Answers

85
votes

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.

9
votes
int x = [TramNumber.text intValue];

You just need to convert NSString to int.

2
votes

You cannot assign text as an integer. You should cast NSString to int like this:

int x = [TramNumber.text intValue];
1
votes

Just try this:

NSString *code = dic[@"code"];
int statusCode = [code intValue];

NSLog(@"%d",statusCode);

if(statusCode==1)//........cont...