我刚刚在 xcode 管理器中找到了 5 个崩溃报告。当我打开它们时,我得到这个堆栈跟踪(标记区域是我的应用程序的名称):
此错误发生在 iOS8.4 和 iOS9 上,同样发生在 iPhone 5 和 iPhone 6 设备上。
我很难找到它,因为我无法在 iPhone5(8.4) 和 iPhone6(9.0.1) 上重现它。
1./2.此处某处:
override func onButtonTableViewCellClick(button: BfPaperButton) {}
3.
var button: BfPaperButton = BfPaperButton.newAutoLayoutView()
func onClick() {
delegate?.onButtonTableViewCellClick(button) // 3
}
我使用 swift 2、xcode 7 和 iOS9。帮助我理解错误。红色图像的第一行是什么意思?为什么这个错误与 Swift.String
完全有关??
我找到了这个线程:https://forums.developer.apple.com/thread/6078我在哪里提取了这些信息:
One case I've seen of this kind of crash is when an Obj-C-based object calls a delegate method that's Swift-based, and a parameter value is nil but the Swift method signature isn't an optional type. In the case I saw, it was an error in the bridged delegate method signature — it was actually supposed to be optional. It could be something similar in your case (the Swift definition doesn't allow for an optional when it should), or it could be a bug in the Obj-C code (producing nil when it shouldn't).
我使用一个名为 BfPaperButton 的 obj-c 库,它创建了一个看起来像 android Material 设计中的按钮。也许错误与这个库有关?
日志:
查看图片:http://i.stack.imgur.com/5aQ8m.png
我有两个字符串扩展。一个用于长度,一个用于子字符串:
extension String {
var length: Int { return self.characters.count }
subscript (i: Int) -> String {
return String(Array(self.characters)[i])
}
subscript (r: Range<Int>) -> String {
let start = startIndex.advancedBy(r.startIndex)
let end = startIndex.advancedBy(r.endIndex)
return substringWithRange(Range(start: start, end: end))
}
}
第 188 行是:
NSUUID().UUIDString[0...4]
使用子字符串扩展。也许错误就在这里?
override func onButtonTableViewCellClick(button: UIButton) {
if let title = button.titleLabel?.text {
if title == "Send code" {
tmpPhoneNumber = ""
var tmp = phoneNumber
if tmp.length > 1 {
if tmp[0] == "0" {
tmp = tmp[1...tmp.characters.count - 1]
}
tmpPhoneNumber = "0049" + tmp
phoneNumberWithCode = tmpPhoneNumber
sendAlert(tmp)
} else {
PfToast.showError("Please enter your phone number.")
}
} else if title == "Finish" {
if let cell: InputTableViewCell = tableView.cellForRowAtIndexPath(NSIndexPath(forRow: 0, inSection: 2)) as? InputTableViewCell {
if isLicenceValid(cell.inputField.inputField.text!) {
createCustomer(cell.inputField.inputField.text!)
} else {
PfToast.showError("Please enter correct licence plate.")
}
}
}
}
}
最佳答案
在我看来,您看错了地方。当我查看日志时,我看到的是:
- 点击一个按钮
onButtonTableViewCellClick
- Obj-C 到 Swift 内部结构(您可以在 What's Dead & Exploded in Swift's exception stack? 中了解更多信息)
- 通过
Int
索引String
(参见String.subscript.getter (Swift.Int) -> String
)
我们可以确定崩溃发生在此处:
return String(Array(self.characters)[i])
我认为我们可以排除 nil
因为那会导致更早的崩溃。您很可能遇到了字符索引越界问题。这意味着 i
为负数或大于 length - 1
(也许您正在索引空文本?)
不幸的是,重要的代码在 onButtonTableViewCellClick
中,而您还没有发布它。
关于ios - swift 拥有保证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32920551/