Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift owned to guaranteed

I just found 5 crash reports inside the xcode organizer. When I open them I get this stacktrace (marked area is the name of my app):

enter image description here

This error occurs on iOS8.4 as well as on iOS9, and on iPhone 5 and iPhone 6 devices likewise.

It is hard for me to track down because I cannot reproduce it neither on iPhone5(8.4) nor on iPhone6(9.0.1).

1./2. somewhere here:

override func onButtonTableViewCellClick(button: BfPaperButton) {}

3.

var button: BfPaperButton = BfPaperButton.newAutoLayoutView()

func onClick() {
    delegate?.onButtonTableViewCellClick(button) // 3
}

I use swift 2, xcode 7 and iOS9. Help me to understand the error. What does the first line with the red image mean? Why has this error something to do with Swift.String at all??

I found this thread: https://forums.developer.apple.com/thread/6078 where I extracted this information:

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).

I use a obj-c lib called BfPaperButton which creates a Button that looks like in android material design. Maybe the error is related to this library?

Log:

enter image description here

View image: http://i.stack.imgur.com/5aQ8m.png

I have two string extensions. One for length and one for substring:

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))
    }
}

Line 188 is:

NSUUID().UUIDString[0...4]

which uses substring extension. Maybe the error is here?

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.")
                    }
                }
            }
        }
    }
like image 669
UpCat Avatar asked Oct 03 '15 08:10

UpCat


1 Answers

In my opinion, you are looking at wrong places. When I am looking at the logs, what I see is:

  1. A click on a button
  2. onButtonTableViewCellClick
  3. Obj-C to Swift internals (you can learn more in What's Dead & Exploded in Swift's exception stack?)
  4. Indexing String by Int (see String.subscript.getter (Swift.Int) -> String)

We can be sure that the crash happened somewhere here:

return String(Array(self.characters)[i])

I think we can rule out nil because that would cause an earlier crash. Most likely you have an character index out of bounds problem. That means i is either negative or higher than length - 1 (maybe you are indexing an empty text?)

Unfortunately, the important code is in onButtonTableViewCellClick and you haven't posted that.

like image 79
Sulthan Avatar answered Oct 27 '22 13:10

Sulthan