Help please - initWithCoder error when I'm not even using one

Discussion in 'iOS Programming' started by whitedragon101, Oct 25, 2017.

  1. whitedragon101, Oct 25, 2017
    Last edited: Oct 25, 2017

    whitedragon101 macrumors 65816

    Joined:
    Sep 11, 2008
    #1
    I just got a message from a user saying the app now crashes when they generate a PDF. I tracked it down and the error is totally weird. No code has changed but after they update to IOS11 this error appears.

    Code:
    2017-10-26 02:04:12.170897+0100 Pocket CBT[63876:4962116] -[PDFViewController initWithCoder:]: unrecognized selector sent to instance 0x60400001aab0
    2017-10-26 02:04:12.180239+0100 Pocket CBT[63876:4962116] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[PDFViewController initWithCoder:]: unrecognized selector sent to instance 0x60400001aab0'
    *** First throw call stack:
    (
        0   CoreFoundation                      0x000000010dd8e1cb __exceptionPreprocess + 171
        1   libobjc.A.dylib                     0x000000010d2a2f41 objc_exception_throw + 48
        2   CoreFoundation                      0x000000010de0e914 -[NSObject(NSObject) doesNotRecognizeSelector:] + 132
        3   CoreFoundation                      0x000000010dd11178 ___forwarding___ + 1432
        4   CoreFoundation                      0x000000010dd10b58 _CF_forwarding_prep_0 + 120
        5   UIKit                               0x000000010af8c278 -[UIClassSwapper initWithCoder:] + 246
        6   UIKit                               0x000000010b1de351 UINibDecoderDecodeObjectForValue + 704
        7   UIKit                               0x000000010b1de086 -[UINibDecoder decodeObjectForKey:] + 89
        8   UIKit                               0x000000010af8bf44 -[UIRuntimeConnection initWithCoder:] + 178
        9   UIKit                               0x000000010b1de351 UINibDecoderDecodeObjectForValue + 704
        10  UIKit                               0x000000010b1de4ef UINibDecoderDecodeObjectForValue + 1118
        11  UIKit                               0x000000010b1de086 -[UINibDecoder decodeObjectForKey:] + 89
        12  UIKit                               0x000000010af8b141 -[UINib instantiateWithOwner:options:] + 1262
        13  UIKit                               0x000000010b418026 -[UIStoryboard instantiateViewControllerWithIdentifier:] + 181
        14  UIKit                               0x000000010b42781a -[UIStoryboardSegueTemplate instantiateOrFindDestinationViewControllerWithSender:] + 90
        15  UIKit                               0x000000010b427a5e -[UIStoryboardSegueTemplate _perform:] + 52
        16  UIKit                               0x000000010aca8418 -[UIViewController performSegueWithIdentifier:sender:] + 99
        17  Pocket CBT                          0x0000000109705a51 -[thoughtRecordInfo showDiagram:] + 97
        18  UIKit                               0x000000010ab01ec9 -[UIApplication sendAction:to:from:forEvent:] + 83
        19  UIKit                               0x000000010ac7f1f6 -[UIControl sendAction:to:forEvent:] + 67
        20  UIKit                               0x000000010ac7f513 -[UIControl _sendActionsForEvents:withEvent:] + 450
        21  UIKit                               0x000000010ac7e440 -[UIControl touchesEnded:withEvent:] + 618
        22  UIKit                               0x000000010ab77b1b -[UIWindow _sendTouchesForEvent:] + 2807
        23  UIKit                               0x000000010ab7923e -[UIWindow sendEvent:] + 4124
        24  UIKit                               0x000000010ab1cd96 -[UIApplication sendEvent:] + 352
        25  UIKit                               0x000000010b45efce __dispatchPreprocessedEventFromEventQueue + 2809
        26  UIKit                               0x000000010b461c23 __handleEventQueueInternal + 5957
        27  CoreFoundation                      0x000000010dd312b1 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
        28  CoreFoundation                      0x000000010ddd0d31 __CFRunLoopDoSource0 + 81
        29  CoreFoundation                      0x000000010dd15c19 __CFRunLoopDoSources0 + 185
        30  CoreFoundation                      0x000000010dd151ff __CFRunLoopRun + 1279
        31  CoreFoundation                      0x000000010dd14a89 CFRunLoopRunSpecific + 409
        32  GraphicsServices                    0x000000010ecd79c6 GSEventRunModal + 62
        33  UIKit                               0x000000010ab0023c UIApplicationMain + 159
        34  Pocket CBT                          0x00000001096ed22f main + 111
        35  libdyld.dylib                       0x000000010f6e3d81 start + 1
    )
    libc++abi.dylib: terminating with uncaught exception of type NSException
    (lldb)
    
    This is the result of a modal segue like this :

    Code:
    [self performSegueWithIdentifier:@"infoToPDF" sender:self];
    the next file is a normal view controller and crashes before it even loads with the error above. The init doesn't even match what I am actually using :

    Code:
    #import "PDFViewController.h"
    #import "AppDelegate.h"
    #import <Foundation/Foundation.h>
    #import "GradientBackgroundLayer.h"
    #import "SummaryVC.h"
    
    @interface PDFViewController ()
    
    @end
    
    @implementation PDFViewController
    
    - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
    {
        self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
        if (self)
        {
            // Custom initialization
        }
        return self;
    }
    

    Any help would be much appreciated.
     
  2. dantastic macrumors 6502a

    dantastic

    Joined:
    Jan 21, 2011
    #2
    initWithCoder is called on a UIViewController that is loaded through a nib or a storyboard. This was always the case But what is strange is that UIViewController has a initWithCoder implementatin already. If you don't override it the app still shouldn't crash...

    Looking at the stack trace though we can see the crash is actually originating from UIClassSwapper. Some interesting info on what that little fella does can be found over here https://gist.github.com/0xced/45daf79b62ad6a20be1c

    This makes me think you might be subclassing something you shouldn't be subclassing?
     
  3. whitedragon101 thread starter macrumors 65816

    Joined:
    Sep 11, 2008
    #3

    The new xCode and IOS11 have just unleashed a ***** storm on me.

    The class is just a UIViewController. I finally managed to fix the problem by literally creating new .m and .h files and a new view controller creating identical layout and hooking things up to the new class where I copied and pasted the code in bit by bit. It now works. The fact that no code changed makes me think there are bugs in xCode/ IOS11.

    What is really a problem is I noticed that the new xCode has completely changed how it interprets my iPad layout (I have both iPhone and iPad storyboards) so all the components are all over the place. So I can't just recompile and submit an update for the error above as it will break the iPad mode which has no problems on the launched version. I have to figure out why the layout has changed but I suspect I have to recreate and re-hook up the whole layout (arrghhhhhh).
     
  4. dantastic macrumors 6502a

    dantastic

    Joined:
    Jan 21, 2011
  5. whitedragon101 thread starter macrumors 65816

    Joined:
    Sep 11, 2008
    #5
    I am downloading Xcode 8.1 . Hopefully that might work. Not sure if you can install an old version of Xcode next to a new one.
     
  6. PhoneyDeveloper macrumors 68040

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #6
    The usual cause for that kind of Unrecognized Selector crash is a bad case of Zombies. I'd recommend that you turn on Zombies and see if anything pops up.

    initWithNibName isn't doing anything with a view controller from a storyboard. You should probably implement initWithCoder in your PDFViewController if you do any custom initialization (and even if you don't).

    It's possible that there was something wrong or old in your storyboard that got updated when you made changes with the new tools.
     
  7. whitedragon101 thread starter macrumors 65816

    Joined:
    Sep 11, 2008
    #7
    Thanks for the help guys.

    After a marathon coding session I fixed the bugs.

    I rebuilt my iPad interface from scratch, can't believe I got it done in 24hours. Feet to the flames is a good motivator. I've never had a bug crash a live app.

    Not sure what the Zombies are but I will definitely google "Zombies in xCode."

    Thanks again
     
  8. thewayfaerer macrumors newbie

    thewayfaerer

    Joined:
    May 14, 2008
    #8
    In case someobody else will come across this, I had exact the same problem with exact the same class name and I renamed it to PDF2ViewController and then it worked. Perhaps it clashes with Apple's PDFKit.
     
  9. PhoneyDeveloper macrumors 68040

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #9
    Hmmm, yes. Two classes with the same name could cause the crashlog showed at the top. Because of the way nibs/storyboards are loaded and the lack of namespaces in obj-c it would be possible for the wrong class to be loaded.

    Apple's Messages framework was also reported to have unadorned class names like Headers and Message, which could cause conflicts.
     

Share This Page