objective-c - 将 NSString 转换为 Integer 并检查它是否小于或等于某个值

标签 objective-c ios xcode nsstring nsinteger

我有一个以 NSString 形式返回距离的项目。我想检查该距离以查看它是小于或等于还是大于 10,000 英尺。我遇到了一个错误,指出“ARC 不允许将‘int’隐式转换为‘NSString *’。”有谁知道如何将 NSString 转换为整数?或者如何构建代码?谢谢你!

- (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;
}

} 

最佳答案

你为什么不试试这个呢?

if ([distanceInFeet intValue]<=10000)
{

if ([distanceInFeet integerValue]<=10000)
{

编辑:

根据您对代码的编辑,并更正显示的错误...

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

关于objective-c - 将 NSString 转换为 Integer 并检查它是否小于或等于某个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13900893/

相关文章:

ios - UIButton上的事件指示器

objective-c - 如何使 NSControl(例如 NSTokenField)忽略鼠标事件

objective-c - 如何在命令行中使用 xcodebuild?

objective-c - Cocoa-Touch - 如何解析本地 Json 文件

ios - 如何制作密码确认器 swift 3

xcode - 如何为所有模拟器启用/禁用 “Show as run destination”

objective-c - 如何在 Objective-C 宏中使用商业 at 符号?

ios - 顶部工具栏和导航栏常用模糊效果

xcode - 运行 Xcode 6.4 应用程序时出现 fatal error

ios - 在后台的 iPhone 7 上未调用应用程序 DidReceiveRemoteNotification