运行 Swift 应用程序时的分段错误 11
Segmentation Fault 11 when running Swift app
我刚刚更新到最新的 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
有人有什么想法吗?显然我已经用 '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
}
这里是 currencyFormatter 的初始化:
let currencyFormatter = NSNumberFormatter()
currencyFormatter.numberStyle = NSNumberFormatterStyle.CurrencyStyle
if let currencyCode = NSLocale.currentLocale().objectForKey(NSLocaleCurrencyCode) as? String {
currencyFormatter.currencyCode = currencyCode
}
我在这里遇到了同样的问题,它与代码本身无关。两天前升级到Apple发布的Xcode 6.3 beta 2后,所有这些都消失了。试一试
这可能不太可能,但是您的 currencyFormatter
是如何声明的?它是否标有 !
而未初始化?我使用以下代码来测试错误声明,它在我的 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 = "3.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 = 3,567.45 vs number = 123567.45
看来,问题出在这一行:
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 崩溃。
我刚刚更新到最新的 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
有人有什么想法吗?显然我已经用 '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
}
这里是 currencyFormatter 的初始化:
let currencyFormatter = NSNumberFormatter()
currencyFormatter.numberStyle = NSNumberFormatterStyle.CurrencyStyle
if let currencyCode = NSLocale.currentLocale().objectForKey(NSLocaleCurrencyCode) as? String {
currencyFormatter.currencyCode = currencyCode
}
我在这里遇到了同样的问题,它与代码本身无关。两天前升级到Apple发布的Xcode 6.3 beta 2后,所有这些都消失了。试一试
这可能不太可能,但是您的 currencyFormatter
是如何声明的?它是否标有 !
而未初始化?我使用以下代码来测试错误声明,它在我的 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 = "3.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 = 3,567.45 vs number = 123567.45
看来,问题出在这一行:
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 崩溃。