问题
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
}
回答1:
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.
回答2:
This might be a long shot, but how is your currencyFormatter declared? Is it marked with an ! and not intialised? I used the following code to test for a faulty declaration and it behaved rather strangely in my 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
If this is your case as well, you need to look into the missing initialisation of the currencyFormatter, and also uncomment the if statements from my code excerpt to avoid it crashing everything. If uncommenting, you'll also see if this is the actual error case for you as there is an else clause stating that the currencyFormatter actually is nil.
The reason for me looking into this case, was to see if you had a problem with unwrapping of currencyFormatter.currencySymbol! and currencyFormatter.decimalSeparator!, but testing revealed they could be nil without casing any other issue than text == "".
You might also want to look into the NSNumberFormatter.numberFromString() method, see following:
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 )")
}
}
which outputs numberStr = $123,567.45 vs number = 123567.45 after converting back and forth.
回答3:
I had the same problem here and it was not related to the code itself. all of them disappeared after upgrading to Xcode 6.3 beta 2 released by Apple two days ago. give it a try
来源:https://stackoverflow.com/questions/28732430/segmentation-fault-11-when-running-swift-app