9

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: https://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.")
                    }
                }
            }
        }
    }
DarkLeafyGreen
  • 69,338
  • 131
  • 383
  • 601
  • What is the actual error message though inside the log viewer? – l'L'l Oct 03 '15 at 08:44
  • @l'L'l What do you mean? I have the organizer view (see my updated question) and when I click on "Open in project" I get the log view like seen in the first picture. – DarkLeafyGreen Oct 03 '15 at 08:51
  • Can you post the entire text of `thread 0, #1` (blanking out private info is fine, it's just that the rest of the line is trucated - which might include the line in which the error occurs). – l'L'l Oct 03 '15 at 08:52
  • @l'L'l see my update (Sadly xcode does not allow to copy logs :/) – DarkLeafyGreen Oct 03 '15 at 09:03
  • I have a feeling it's the optional value that is associated with the `onclick()` function returning nil thus causing the exception. One more question, what is on line 188? – l'L'l Oct 03 '15 at 09:09
  • @l'L'l see my update – DarkLeafyGreen Oct 03 '15 at 09:18
  • Unfortunately it's difficult to tell what exactly is causing it without the actual exception message; crash stacks can only reveal so much. Setting up "symbolicating" can help with this type of thing tenfold — [see this topic](http://stackoverflow.com/questions/1460892/symbolicating-iphone-app-crash-reports), as you can get the specifics. – l'L'l Oct 03 '15 at 09:27
  • You might want to NSLog the result of `UUIDString`, and possibly the substring function to see if it ever returns nil (the exception `unexpectedly found nil while unwrapping an Optional Value` is commonly associated with the issue you have) – l'L'l Oct 03 '15 at 09:40
  • @l'L'l I will check this out, thank you for your help :) – DarkLeafyGreen Oct 03 '15 at 09:41
  • Are you sure you haven't fixed the bug already? How old are the logs? That `tmp[0]` seems to be the only possibility for the crash but you are handling the `tmp.length == 0` case correctly so I can't imagine how the crash could happen with the current code. – Sulthan Oct 26 '15 at 09:41
  • `onButtonTableViewCellClick` is overridable, is the one your paste here the one in the `VerificationController`? – Cosyn Oct 26 '15 at 12:17
  • What is on line 168? Is it perhaps `tmp = tmp[1...tmp.characters.count - 1]`? – Paul Cantrell Oct 28 '15 at 05:53
  • Can you add the code around line 188? – djromero Oct 30 '15 at 18:57
  • 1
    The 168 and 188 are irrelevant. Those are machine code offsets, not line numbers. We can tell from stack trace that your crash is in `subscript (i: Int) -> String`, called from `onButtonTableViewCellClick`. The only use of that `subcript` in that caller is the line `if tmp[0] == "0" {`. Is it possible that your string starts with a "funny" Unicode character that is represented by a surrogate pair? – rob mayoff Oct 30 '15 at 23:39

1 Answers1

9

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.

Community
  • 1
  • 1
Sulthan
  • 128,090
  • 22
  • 218
  • 270