Segmentation Error 11 when starting the Swift application

I just upgraded to the newest version of Xcode 6.3 and I get an error message that I cannot solve.

When I run my application, I get an error message: Command failed due to signal: Segmentation Fault 11. I looked at the full error message, and I accepted the details that, in my opinion, 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

Does anyone have any ideas? Obviously, I replaced the full path with "Filename". This is the error code:

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 initialization of currencyFormatter:

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

    if let currencyCode = NSLocale.currentLocale().objectForKey(NSLocaleCurrencyCode) as? String {
        currencyFormatter.currencyCode = currencyCode
    }
0
source share
3 answers

There seems to be a problem in this line:

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

This is a compiler error. The workarounds I found are as follows:

// 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 reproducing this problem will be as follows:

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.

+6

, currencyFormatter? !, intialised? , Swift iOS:

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 .

+1

I had the same problem and it was not related to the code itself. they all disappeared after upgrading to Xcode 6.3 beta 2 released by Apple two days ago. try

0
source

All Articles