use*_*428 0 segmentation-fault ios swift
我刚刚更新到最新的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))
Run Code Online (Sandbox Code Playgroud)
和
While emitting SIL for 'textFieldDidChangeValue' at /Filename/FifthViewController.swift:418:5
Run Code Online (Sandbox Code Playgroud)
有人有主意吗?显然我已经用'Filename'替换了完整路径.这是与错误相关的代码:
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
}
Run Code Online (Sandbox Code Playgroud)
这是currencyFormatter的初始化:
let currencyFormatter = NSNumberFormatter()
currencyFormatter.numberStyle = NSNumberFormatterStyle.CurrencyStyle
if let currencyCode = NSLocale.currentLocale().objectForKey(NSLocaleCurrencyCode) as? String {
currencyFormatter.currencyCode = currencyCode
}
Run Code Online (Sandbox Code Playgroud)
看来,问题出在这一行:
textField.text = currencyFormatter.stringFromNumber((text as NSString).doubleValue / 100.0)
Run Code Online (Sandbox Code Playgroud)
它必须是编译器错误.我找到的解决方法是:
// 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)
Run Code Online (Sandbox Code Playgroud)
重现此问题的最小代码是:
let str = "42"
let a:NSNumber = (str as NSString).doubleValue / 100.0
Run Code Online (Sandbox Code Playgroud)
Swift 1.1/Xcode 6.1.1成功编译,但Swift 1.2/Xcode 6.3 Beta2崩溃.
| 归档时间: |
|
| 查看次数: |
1579 次 |
| 最近记录: |