运行Swift app时出现Segmentation Fault 11

时间:2015-02-26 00:33:29

标签: ios swift segmentation-fault

我刚刚更新到最新的Xcode 6.3测试版,我收到一个错误,我无法找到解决方案。

当我运行我的应用时,我收到错误:Command failed due to signal: Segmentation Fault 11。我查看了完整的错误消息,并且我已经采取了我认为相关的部分:

(unresolved_dot_expr type='@lvalue String!' location=/Filename/FifthViewController.swift:423:19 range=[/Filename/FifthViewController.swift:423:9 - line:423:19] field 'text'
  (declref_expr type='UITextField' location=/Filename/FifthViewController.swift:423:9 range=[/Filename/FifthViewController.swift:423:9 - line:423:9] decl=AffordIt.(file).FifthViewController.func decl.textField@/Filename/FifthViewController.swift:418:34 specialized=yes))

并且

While emitting SIL for 'textFieldDidChangeValue' at /Filename/FifthViewController.swift:418:5

有人有什么想法吗?显然,我已经用'文件名'取代了完整的路径。这是与错误相关的代码:

textField.addTarget(self, action: "textFieldDidChangeValue:", forControlEvents: UIControlEvents.EditingChanged)

func textFieldDidChangeValue(textField: UITextField) {
    //Automatic formatting for transaction value text field. Target is added above.
    var text = textField.text.stringByReplacingOccurrencesOfString(currencyFormatter.currencySymbol!, withString: "").stringByReplacingOccurrencesOfString(currencyFormatter.groupingSeparator, withString: "").stringByReplacingOccurrencesOfString(currencyFormatter.decimalSeparator!, withString: "").stringByReplacingOccurrencesOfString(" ", withString: "") // There is a special character here. This line is critical for european/other currencies.
    println(textField.text)

    textField.text = currencyFormatter.stringFromNumber((text as NSString).doubleValue / 100.0)
    currencyDouble = (text as NSString).doubleValue / 100.0
    valueEnter.alpha = 1
}

这是currencyFormatter的初始化:

    let currencyFormatter = NSNumberFormatter()
    currencyFormatter.numberStyle = NSNumberFormatterStyle.CurrencyStyle

    if let currencyCode = NSLocale.currentLocale().objectForKey(NSLocaleCurrencyCode) as? String {
        currencyFormatter.currencyCode = currencyCode
    }

3 个答案:

答案 0 :(得分:6)

看来,问题出在这一行:

textField.text = currencyFormatter.stringFromNumber((text as NSString).doubleValue / 100.0)

它必须是编译器错误。我找到的解决方法是:

// Explicitly cast as `NSNumber`
textField.text = currencyFormatter.stringFromNumber((text as NSString).doubleValue / 100.0 as NSNumber)

// or explicitly construct `NSNumber` from `Double` 
textField.text = currencyFormatter.stringFromNumber(NSNumber(double: (text as NSString).doubleValue / 100.0))

// or prepare `Double` outside
let doubleVal = (text as NSString).doubleValue / 100.0
textField.text = currencyFormatter.stringFromNumber(doubleVal)

// or convert `String` to `Double` without casting to `NSString`.
textField.text = currencyFormatter.stringFromNumber( atof(text) / 100.0)

重现此问题的最小代码是:

let str = "42"
let a:NSNumber = (str as NSString).doubleValue / 100.0

Swift 1.1 / Xcode 6.1.1成功编译,但Swift 1.2 / Xcode 6.3 Beta2崩溃。

答案 1 :(得分:1)

这可能是一个长镜头,但你的currencyFormatter是如何宣布的?它是否标有!并且没有初始化?我使用以下代码来测试一个错误的声明,它在我的Swift iOS Playground中表现得相当奇怪:

var myTextField = UITextField()
var currencyFormatter : NSNumberFormatter!  // Possible offender! Not initiliased, and forcefully unwrapped!!!

func myFunc(textField: UITextField) {
    //Automatic formatting for transaction value text field. Target is added above.
    var text : String = textField.text // Error: Execution was interrupted, reason: EXC_BAD_INSTRUCTION


    //if currencyFormatter !=  nil {
        text = textField.text
           .stringByReplacingOccurrencesOfString(currencyFormatter.currencySymbol!, withString: "")
           .stringByReplacingOccurrencesOfString(currencyFormatter.groupingSeparator, withString: "")
           .stringByReplacingOccurrencesOfString(currencyFormatter.decimalSeparator!, withString: "")
           .stringByReplacingOccurrencesOfString(" ", withString: "") // There is a special character here. This line is critical for european/other currencies.

           println("original: \(textField.text), after replacing: \(text)")
           textField.text = currencyFormatter.stringFromNumber((text as NSString).doubleValue / 100.0)
           println("new: \(textField.text)")

           // currencyDouble = (text as NSString).doubleValue / 100.0
           // valueEnter.alpha = 1
     // } else {
     //    println("No currencyFormatter")
     // }

}

//currencyFormatter = NSNumberFormatter()
// Uncomment line above, and the playground failed on second line in myFunc()
myTextField.text = "$123.000,00"

myFunc(myTextField)
myTextField.text

如果这也是你的情况,你需要查看currencyFormatter缺少的初始化,并从我的代码摘录中取消注释if语句,以避免它崩溃。如果取消注释,您还会看到这是否是您的实际错误案例,因为有else条款声明currencyFormatter实际上是nil

我调查此案例的原因是,看看你是否在解开currencyFormatter.currencySymbol!currencyFormatter.decimalSeparator!时遇到了问题,但测试显示他们可能nil没有任何其他问题问题比text == ""

您可能还想查看NSNumberFormatter.numberFromString()方法,请参阅以下内容:

var currencyFormatter = NSNumberFormatter()
currencyFormatter.numberStyle = NSNumberFormatterStyle.CurrencyStyle

if let numberStr = currencyFormatter.stringFromNumber(NSNumber(double: 123567.45)) {

    if let number = currencyFormatter.numberFromString(numberStr) {
        println("numberStr = \(numberStr) vs number = \( number )")
    }
}

在来回转换后输出numberStr = $123,567.45 vs number = 123567.45

答案 2 :(得分:0)

我在这里遇到了同样的问题,它与代码本身无关。两天前升级到Apple发布的Xcode 6.3 beta 2后,所有这些都消失了。试一试