Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
247 views
in Technique[技术] by (71.8m points)

ios - Segmentation Fault 11 when running Swift app

I have just updated to the newest Xcode 6.3 beta and I am receiving an error that I can't figure out the solution to.

When I run my app, I am getting the error: Command failed due to signal: Segmentation Fault 11. I had a look through the full error message and I have taken the parts out that I think are relevant:

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

And

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

Anyone have any ideas? Obviously I've replaced the full path with 'Filename'. This is the code related to the error:

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
}

Here is the initialisation of currencyFormatter:

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

    if let currencyCode = NSLocale.currentLocale().objectForKey(NSLocaleCurrencyCode) as? String {
        currencyFormatter.currencyCode = currencyCode
    }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It seems, the problem is in this line:

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

It must be a compiler bug. The workarounds I found are:

// 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)

The minimum code that reproduces this problem would be:

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

Swift 1.1/Xcode 6.1.1 compiles it successfully, but Swift 1.2/Xcode 6.3 Beta2 crashes.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...