EXC_Breakpoint when not forcefully unwrapping
Asked Answered
V

1

0

I'm getting a crash EXC_Breakpoint which would indicate to me that there is a nil variable being unwrapped?

The line of code it is pointing to is the definition of let obHeight:

if let qs = Helper_Question.getSection(sid: self.sections[0].sid) {
   let obEnabled = Helper_Type.getId(id: tid)?.enableObservation == 1
   for q in qs {
       let ans = Helper_Answer.getQuestion(aid: selectedAid!, qid: q.Id)
       var baseHeight : CGFloat = 300
       switch(q.qtype){
       case Constants.DEFAULT:
            let obHeight : CGFloat = (obEnabled || ans?.comment != "" || (ans?.answer == "1" && q.hasChildren == 0) || (ans?.answer == "2" && q.hasChildren == 0)) && ans?._na == 0 ? 40 : 0
             ....
        }
    }
}

I'm using the ? instead of forcefully unwrapping so I don't think this is the issue? This crash report came into today but I cannot reproduce it on any device. What other reason apart from a nil variable would cause a EXC_Breakpoint crash? This code is called on action of a button click in a table view cell and uses a delegate to call this code to do some calculations before presenting a splitviewcontroller. selectedAid comes from the button's accessibility identifier which i am setting in the cell for row, always. it is not nil

crash log:

Crashed: com.apple.main-thread
    0  AppName                        0x100664b30 specialized AuditVC.editBtnTapped(cell : PendingAuditTVC) -> () (AuditVC.swift:294)
    1  AppName                        0x10065fa68 AuditVCAppNamePendingAuditCellDelegate (AuditVC.swift)
    2  AppName                        0x100717468 PendingAuditTVC.editTapped(Any) -> () (PendingAuditTVC.swift:41)
    3  AppName                        0x100717648 @objc PendingAuditTVC.editTapped(Any) -> () + 4302730824
    4  UIKit                          0x1907c6010 -[UIApplication sendAction:to:from:forEvent:] + 96
    5  UIKit                          0x1907c5f90 -[UIControl sendAction:to:forEvent:] + 80
    6  UIKit                          0x1907b0504 -[UIControl _sendActionsForEvents:withEvent:] + 440
    7  UIKit                          0x1907c5874 -[UIControl touchesEnded:withEvent:] + 576
    8  UIKit                          0x190d5a550 _UIGestureEnvironmentSortAndSendDelayedTouches + 4280
    9  UIKit                          0x190d5689c _UIGestureEnvironmentUpdate + 1128
    10 UIKit                          0x190d563e0 -[UIGestureEnvironment _deliverEvent:toGestureRecognizers:usingBlock:] + 404
    11 UIKit                          0x190d5568c -[UIGestureEnvironment _updateGesturesForEvent:window:] + 268
    12 UIKit                          0x1907c070c -[UIWindow sendEvent:] + 3164
    13 UIKit                          0x19079133c -[UIApplication sendEvent:] + 340
    14 UIKit                          0x190f8b014 __dispatchPreprocessedEventFromEventQueue + 2400
    15 UIKit                          0x190f85770 __handleEventQueue + 4268
    16 UIKit                          0x190f85b9c __handleHIDEventFetcherDrain + 148
    17 CoreFoundation                 0x18a61542c __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 24
    18 CoreFoundation                 0x18a614d9c __CFRunLoopDoSources0 + 540
    19 CoreFoundation                 0x18a6129a8 __CFRunLoopRun + 744
    20 CoreFoundation                 0x18a542da4 CFRunLoopRunSpecific + 424
    21 GraphicsServices               0x18bfac074 GSEventRunModal + 100
    22 UIKit                          0x1907f6058 UIApplicationMain + 208
    23 AppName                    0x100044590 main (AppDelegate.swift:23)
    24 libdyld.dylib                  0x18955159c start + 4

UPDATE

open class Question
{
    open var Id : Int = 0
    open var sectionid : Int = 0
    open var question : String = ""
    open var hasChildren : Int = 0
    open var qtype : Int = 0
    open var defaultValue : String = ""
}

public class UP_IA_Answer_m : EVObject {

    public var localId : String = ""
    public var aid : String = ""
    public var sid : Int = 0
    public var qid : Int = 0
    public var answer : String = ""
    public var comment : String = ""
    public var hasChildren : Int = 0
    public var _na : Int = 0
}
Volunteer answered 7/2, 2018 at 12:16 Comment(12)
Not related to your problem, but ? true : false is unnecessary, leave it out.Urania
@the4kman done. No idea on the problem? I had 4 crashes with one user.Volunteer
Are you sure that q is not nil?Urania
It would be helpful to see the models for Question and Answer, which I assume is the model names...Chesterfieldian
@the4kman q is not nil, no.. as long as qs returns value, none of those values are nil.Volunteer
@Chesterfieldian I'll add these now but if you're looking for optional properties, there isn't anyVolunteer
Are you sure you haven't defined a breakpoint in Xcode at the position of let obHeight ... ?Chesterfieldian
@Chesterfieldian I don't have a breakpoint there right now anyway, if I had had a breakpoint in xCode at the time of archiving and submitting, would that cause a crash from app users using the app from the AppStore??Volunteer
Breakpoints aren't a part of the production build, no.Chesterfieldian
@Chesterfieldian ok, great. I was very confused by the question so :)Volunteer
@Chesterfieldian If sections had a count of zero and so sections[0].sid failed, would I expect a exc_breakpoint and perhaps Crashlytics pointed to the wrong line number?Volunteer
Well.. that could cause some problems, yes. Make sure you check that the array in fact contains an item at the specific index before trying to access it.Chesterfieldian
A
-2

Replacing the values and variable names of the one-liner with "x" to make the error easier to spot:

(x || x?.x != x || (x?.x == x && x == x) || (x?.x == x && x == x)) && x?.x == x ? x : x

I think it is pretty clear where the error happens.

Armington answered 7/2, 2018 at 12:47 Comment(3)
it's just too darn long for one line? or there is an explicit error I'm missing?Volunteer
if it is that it's just too long will splitting it up into one if-else, really change things? I still have to check all of the conditions to the left of the ? and then slot in the assignment for the if and elseVolunteer
This doesn't really answer the question, please elaborate your answer to make it more useful to the OP.Chesterfieldian

© 2022 - 2024 — McMap. All rights reserved.