iOS应用程序在手机上崩溃,但在模拟器上运行正常 [英] iOS App Crashes on Phone but works fine on simulator

查看:301
本文介绍了iOS应用程序在手机上崩溃,但在模拟器上运行正常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

var displayValue: Double{
    get{
        println("display.text =\(display.text!)")
        return NSNumberFormatter().numberFromString(display.text!)!.doubleValue
    }
    set{
        display.text = "\(newValue)"
        userIsInTheMiddleOfTypingANumber = false;
    }
}

它在模拟器中工作正常。但是当我在手机上试用它时会崩溃。
这里是控制台:

It works fine in the simulator. but when I try it on phone it crashes. here is the console:

digit= 3
display.text =3
operandStack =[3.0]
digit= 2
display.text =2
operandStack =[3.0, 2.0]
display.text =6.0
fatal error: unexpectedly found nil while unwrapping an Optional value

这一行:

NSNumberFormatter()。numberFromString(display.text!)!

NSNumberFormatter().numberFromString(display.text!)!

返回nil,导致应用程序崩溃,导致无法打开可选项。我真的不知道出了什么问题。我正在关注iTunes U中的一些教程。

is returning nil which causing the app to crash cause it couldn't unwrap the optional. I really don't know what's wrong. I'm following some tutorials in iTunes U.

任何帮助都将受到赞赏。

any help would be appreciated.

推荐答案

尝试:

get{
    println("display.text =\(display.text!)")
    let formatter = NSNumberFormatter()
    formatter.locale = NSLocale(localeIdentifier: "en_US_POSIX")
    return formatter.numberFromString(display.text!)!.doubleValue
}

因为, NSNumberFormatter 使用设备默认情况下,locale可能是小数点分隔符不是。例如:

Because, NSNumberFormatter uses devices locale by default, it's possible that the decimal separator is not ".". For example:

let formatter = NSNumberFormatter()
formatter.locale = NSLocale(localeIdentifier: "ar-SA")
print(formatter.decimalSeparator!) // -> outputs "٫"
formatter.numberFromString("6.0") // -> nil

使用此类语言环境的格式化程序无法解析6.0之类的字符串。因此,如果您希望格式化程序获得一致的结果,则应明确指定区域设置

The formatter that uses such locales cannot parse strings like "6.0". So if you want consistent result from the formatter, you should explicitly specify the locale.

至于 en_US_POSIX 区域设置,请参阅文档

As for en_US_POSIX locale, see the document:


在大多数情况下,最好的区域设置是 en_US_POSIX ,一个专门设计用于产生美国英语结果的语言环境,无论用户和系统偏好如何。 en_US_POSIX 在时间上也是不变的(如果美国在将来某个时候改变日期格式, en_US 将更改以反映新行为,但 en_US_POSIX 将不会),并且平台之间( en_US_POSIX 的工作方式相同在iPhone OS上,就像在OS X上一样,和在其他平台上一样)。

In most cases the best locale to choose is en_US_POSIX, a locale that's specifically designed to yield US English results regardless of both user and system preferences. en_US_POSIX is also invariant in time (if the US, at some point in the future, changes the way it formats dates, en_US will change to reflect the new behavior, but en_US_POSIX will not), and between platforms (en_US_POSIX works the same on iPhone OS as it does on OS X, and as it does on other platforms).

这篇关于iOS应用程序在手机上崩溃,但在模拟器上运行正常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆