ios 将 NSString 更改为 int 值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26336373/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 03:09:32  来源:igfitidea点击:

Change NSString to int value?

iosobjective-c

提问by needshelp

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 *'

使用类型为“NSString *”的表达式初始化“int”的指向整数转换的不兼容指针

- (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.

请帮忙。如果有帮助,我在 iOS 上运行 xCode 5.1.1。

回答by Oleg Gordiichuk

You are representing NSStringvalue wrong.

你代表的NSString价值是错误的。

Use this code sample to solve your problem:

使用此代码示例来解决您的问题:

int x = [TramNumber.text intValue];

To represent int value from your textfield.

表示文本字段中的 int 值。

回答by Reconquistador

int x = [TramNumber.text intValue];

You just need to convert NSString to int.

您只需要将 NSString 转换为 int。

回答by SteBra

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

您不能将文本指定为整数。您应该像这样将 NSString 转换为 int:

int x = [TramNumber.text intValue];

回答by iAj

Just try this:

试试这个:

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

NSLog(@"%d",statusCode);

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