What calls init?(coder aDecoder)?

I know when you subclass a UIview (making your own custom view) and you try to implement a custom initializer you have to implement the initwithcoder init because it's required by the super class (UIview conforms to NSCoding protocol). But what I'm trying to understand is what calls this method?


Right now I have this situation going on:

- I've created a custom view subclass and added it as a subview programmatically to the UI/storyboard. I also have the view displaying on the storyboard but in it's identity inspector I have it showing that it is of the type of my custom view.


So to reiterate, my question is, what call init?coder a decoder? And what is the process? What goes on?


Anything enlightening will be appreciated,


Thank you

Replies

If you are just wondering about the unachieving process of a view from a xib, I would start by looking through the documentation and watching some of the WWDC videos. If there is something specifically you are wondering about can you please clarify what the scenario is including the expected behavior and actual behavior.

initWithCoder is called by UIKit when it instantiates a view object from a storyboard or XIB.


> added it as a subview programmatically to the UI/storyboard


That statement makes little sense. You can't add something programmatically to the storyboard. You can add it programmatically to the view hierarchy at runtime, or you add it manually (non-programmatically) to the storyboard at build time. If you're adding subviews in code at runtime, you can use whatever constructor you like. Most likely not initWithCoder - more likely initWithFrame or some other custom initializer you define. But if you have added it to the storyboard, then UIKit will call initWithCoder on the specified custom class when creating the view.

I am also trying to understand in what exact scenarios does the UIKit call init?(coder:) method. I have an app written completely programmatically (from top to bottom, except for LaunchScreen.storyboard), I gather from Crashlytics that in some rare scenario my custom UIView class's `required init?(coder)` is getting called and resulting in `fatalError("init(coder:) has not been implemented")` crash. Can someone here please help me understand why this method would get called (in some rare scenario, not everytime) when I am not at all using storyboard / xibs in my whole project?


Below is my Crashlog,


# Crashlytics - plaintext stacktrace downloaded by Nagendra Rao at Thu, 18 Oct 2018 10:19:13 GMT
# URL: https://fabric.io/myapp/ios/apps/com.app.myapp/issues/5bc77ad6f8b88c29632bf88c?time=last-seven-days/sessions/b9facb25dd754a9fb14cdf0c4f4fffb1_DNE_0_v2
# Organization: MyOrg
# Platform: ios
# Application: MyApp
# Version: 1.1.8 (2)
# Bundle Identifier: com.app.myapp
# Issue #: 104
# Issue ID: 5bc77ad6f8b88c29632bf88c
# Session ID: b9facb25dd754a9fb14cdf0c4f4fffb1_DNE_0_v2
# Date: 2018-10-17T18:09:00Z
# OS Version: 12.0.0 (16A366)
# Device: iPhone 6s Plus
# RAM Free: 4.1%
# Disk Free: 9.6%


#0. Crashed: com.apple.main-thread
0  libswiftCore.dylib             0x1035ee478 specialized _assertionFailure(_:_:file:line:flags:) + 42360
1  libswiftCore.dylib             0x1034560f4 _assertionFailure(_:_:file:line:flags:) + 135288
2  MyApp                0x1010f59f4 @objc HeaderViewinit(_:) (HeaderView.swift:35)
3  Foundation                     0x19a8b9b9c _decodeObjectBinary + 2000
4  Foundation                     0x19a80d924 -[NSKeyedUnarchiver _decodeArrayOfObjectsForKey:] + 1412
5  Foundation                     0x19a7e0a78 -[NSArray(NSArray) initWithCoder:] + 220
6  Foundation                     0x19a8b9b9c _decodeObjectBinary + 2000
7  Foundation                     0x19a8b9100 _decodeObject + 336
8  Foundation                     0x19a7c6260 -[NSKeyedUnarchiver decodeObjectForKey:] + 224
9  UIKitCore                      0x1c6d42570 -[UIView initWithCoder:] + 652
10 UIKitCore                      0x1c68e322c -[UIImageView initWithCoder:] + 92
11 UIKitCore                      0x1c6aefc9c -[UISegment initWithCoder:] + 72
12 Foundation                     0x19a8b9b9c _decodeObjectBinary + 2000
13 Foundation                     0x19a8b9100 _decodeObject + 336
14 Foundation                     0x19a7c6260 -[NSKeyedUnarchiver decodeObjectForKey:] + 224
15 UIKitCore                      0x1c6af6970 -[UIAccessibilityLargeTextSegmentedViewController segmentFromSegment:] + 164
16 UIKitCore                      0x1c6af6f00 -[UIAccessibilityLargeTextSegmentedViewController initWithSegmentedControl:segments:delegate:] + 1076
17 UIKitCore                      0x1c6afd630 -[UISegmentedControl _axLongPressHandler:] + 292
18 UIKitCore                      0x1c68a504c -[UIGestureRecognizerTarget _sendActionWithGestureRecognizer:] + 64
19 UIKitCore                      0x1c68ad668 _UIGestureRecognizerSendTargetActions + 124
20 UIKitCore                      0x1c68aaeec _UIGestureRecognizerSendActions + 320
21 UIKitCore                      0x1c68aa3d4 -[UIGestureRecognizer _updateGestureWithEvent:buttonEvent:] + 764
22 UIKitCore                      0x1c689d6dc _UIGestureEnvironmentUpdate + 1096
23 CoreFoundation                 0x199dcdb94 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 32
24 CoreFoundation                 0x199dc8828 __CFRunLoopDoObservers + 412
25 CoreFoundation                 0x199dc8dc8 __CFRunLoopRun + 1264
26 CoreFoundation                 0x199dc85b8 CFRunLoopRunSpecific + 436
27 GraphicsServices               0x19c03c584 GSEventRunModal + 100
28 UIKitCore                      0x1c66c3558 UIApplicationMain + 212
29 MyApp                0x100fe9b08 main (AppDelegate.swift:25)
30 libdyld.dylib                  0x199888b94 start + 4


--


#0. Crashed: com.apple.main-thread
0  libswiftCore.dylib             0x1035ee478 specialized _assertionFailure(_:_:file:line:flags:) + 42360
1  libswiftCore.dylib             0x1034560f4 _assertionFailure(_:_:file:line:flags:) + 135288
2  MyApp                0x1010f59f4 @objc HeaderViewinit(_:) (HeaderView.swift:35)
3  Foundation                     0x19a8b9b9c _decodeObjectBinary + 2000
4  Foundation                     0x19a80d924 -[NSKeyedUnarchiver _decodeArrayOfObjectsForKey:] + 1412
5  Foundation                     0x19a7e0a78 -[NSArray(NSArray) initWithCoder:] + 220
6  Foundation                     0x19a8b9b9c _decodeObjectBinary + 2000
7  Foundation                     0x19a8b9100 _decodeObject + 336
8  Foundation                     0x19a7c6260 -[NSKeyedUnarchiver decodeObjectForKey:] + 224
9  UIKitCore                      0x1c6d42570 -[UIView initWithCoder:] + 652
10 UIKitCore                      0x1c68e322c -[UIImageView initWithCoder:] + 92
11 UIKitCore                      0x1c6aefc9c -[UISegment initWithCoder:] + 72
12 Foundation                     0x19a8b9b9c _decodeObjectBinary + 2000
13 Foundation                     0x19a8b9100 _decodeObject + 336
14 Foundation                     0x19a7c6260 -[NSKeyedUnarchiver decodeObjectForKey:] + 224
15 UIKitCore                      0x1c6af6970 -[UIAccessibilityLargeTextSegmentedViewController segmentFromSegment:] + 164
16 UIKitCore                      0x1c6af6f00 -[UIAccessibilityLargeTextSegmentedViewController initWithSegmentedControl:segments:delegate:] + 1076
17 UIKitCore                      0x1c6afd630 -[UISegmentedControl _axLongPressHandler:] + 292
18 UIKitCore                      0x1c68a504c -[UIGestureRecognizerTarget _sendActionWithGestureRecognizer:] + 64
19 UIKitCore                      0x1c68ad668 _UIGestureRecognizerSendTargetActions + 124
20 UIKitCore                      0x1c68aaeec _UIGestureRecognizerSendActions + 320
21 UIKitCore                      0x1c68aa3d4 -[UIGestureRecognizer _updateGestureWithEvent:buttonEvent:] + 764
22 UIKitCore                      0x1c689d6dc _UIGestureEnvironmentUpdate + 1096
23 CoreFoundation                 0x199dcdb94 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 32
24 CoreFoundation                 0x199dc8828 __CFRunLoopDoObservers + 412
25 CoreFoundation                 0x199dc8dc8 __CFRunLoopRun + 1264
26 CoreFoundation                 0x199dc85b8 CFRunLoopRunSpecific + 436
27 GraphicsServices               0x19c03c584 GSEventRunModal + 100
28 UIKitCore                      0x1c66c3558 UIApplicationMain + 212
29 MyApp                0x100fe9b08 main (AppDelegate.swift:25)
30 libdyld.dylib                  0x199888b94 start + 4


#1. com.apple.uikit.eventfetch-thread
0  libsystem_kernel.dylib         0x1999c9ed0 mach_msg_trap + 8
1  libsystem_kernel.dylib         0x1999c93a8 mach_msg + 72
2  CoreFoundation                 0x199dcdfb0 __CFRunLoopServiceMachPort + 236
3  CoreFoundation                 0x199dc8e4c __CFRunLoopRun + 1396
4  CoreFoundation                 0x199dc85b8 CFRunLoopRunSpecific + 436
5  Foundation                     0x19a7bc6a4 -[NSRunLoop(NSRunLoop) runMode:beforeDate:] + 300
6  Foundation                     0x19a7bc550 -[NSRunLoop(NSRunLoop) runUntilDate:] + 148
7  UIKitCore                      0x1c6620ac0 -[UIEventFetcher threadMain] + 136
8  Foundation                     0x19a8ef3b0 __NSThread__start__ + 1040
9  libsystem_pthread.dylib        0x199a592fc _pthread_body + 128
10 libsystem_pthread.dylib        0x199a5925c _pthread_start + 48
11 libsystem_pthread.dylib        0x199a5cd08 thread_start + 4


#2. Thread
0  libsystem_kernel.dylib         0x1999d5b9c __workq_kernreturn + 8
1  libsystem_pthread.dylib        0x199a5a114 _pthread_wqthread + 348
2  libsystem_pthread.dylib        0x199a5cd00 start_wqthread + 4


#3. Thread
0  libsystem_kernel.dylib         0x1999d5428 __semwait_signal + 8
1  libsystem_c.dylib              0x19994a5d0 nanosleep + 212
2  libsystem_c.dylib              0x19994a3cc sleep + 44
3  BugfenderSDK                   0x101985048 monitorCachedData + 632
4  libsystem_pthread.dylib        0x199a592fc _pthread_body + 128
5  libsystem_pthread.dylib        0x199a5925c _pthread_start + 48
6  libsystem_pthread.dylib        0x199a5cd08 thread_start + 4


#4. KSCrash Exception Handler (Secondary)
0  libsystem_kernel.dylib         0x1999c9ed0 mach_msg_trap + 8
1  libsystem_kernel.dylib         0x1999c93a8 mach_msg + 72
2  libsystem_kernel.dylib         0x1999c56b8 thread_suspend + 84
3  BugfenderSDK                   0x1019858d4 handleExceptions + 124
4  libsystem_pthread.dylib        0x199a592fc _pthread_body + 128
5  libsystem_pthread.dylib        0x199a5925c _pthread_start + 48
6  libsystem_pthread.dylib        0x199a5cd08 thread_start + 4


#5. KSCrash Exception Handler (Primary)
0  libsystem_kernel.dylib         0x1999c9ed0 mach_msg_trap + 8
1  libsystem_kernel.dylib         0x1999c93a8 mach_msg + 72
2  BugfenderSDK                   0x101985900 handleExceptions + 168
3  libsystem_pthread.dylib        0x199a592fc _pthread_body + 128
4  libsystem_pthread.dylib        0x199a5925c _pthread_start + 48
5  libsystem_pthread.dylib        0x199a5cd08 thread_start + 4


#6. com.twitter.crashlytics.ios.MachExceptionServer
0  MyApp                0x101151d00 CLSProcessRecordAllThreads + 4296596736
1  MyApp                0x1011521c0 CLSProcessRecordAllThreads + 4296597952
2  MyApp                0x101141a04 CLSHandler + 4296530436
3  MyApp                0x10113cdf4 CLSMachExceptionServer + 4296510964
4  libsystem_pthread.dylib        0x199a592fc _pthread_body + 128
5  libsystem_pthread.dylib        0x199a5925c _pthread_start + 48
6  libsystem_pthread.dylib        0x199a5cd08 thread_start + 4


#7. Thread
0  libsystem_kernel.dylib         0x1999d5b9c __workq_kernreturn + 8
1  libsystem_pthread.dylib        0x199a5a114 _pthread_wqthread + 348
2  libsystem_pthread.dylib        0x199a5cd00 start_wqthread + 4


#8. com.apple.CFSocket.private
0  libsystem_kernel.dylib         0x1999d5350 __select + 8
1  CoreFoundation                 0x199dd6e00 __CFSocketManager + 620
2  libsystem_pthread.dylib        0x199a592fc _pthread_body + 128
3  libsystem_pthread.dylib        0x199a5925c _pthread_start + 48
4  libsystem_pthread.dylib        0x199a5cd08 thread_start + 4


#9. com.apple.NSURLConnectionLoader
0  libsystem_kernel.dylib         0x1999c9ed0 mach_msg_trap + 8
1  libsystem_kernel.dylib         0x1999c93a8 mach_msg + 72
2  CoreFoundation                 0x199dcdfb0 __CFRunLoopServiceMachPort + 236
3  CoreFoundation                 0x199dc8e4c __CFRunLoopRun + 1396
4  CoreFoundation                 0x199dc85b8 CFRunLoopRunSpecific + 436
5  CFNetwork                      0x19a3e8e18 -[__CoreSchedulingSetRunnable runForever] + 212
6  Foundation                     0x19a8ef3b0 __NSThread__start__ + 1040
7  libsystem_pthread.dylib        0x199a592fc _pthread_body + 128
8  libsystem_pthread.dylib        0x199a5925c _pthread_start + 48
9  libsystem_pthread.dylib        0x199a5cd08 thread_start + 4


#10. com.squareup.SocketRocket.NetworkThread
0  libsystem_kernel.dylib         0x1999c9ed0 mach_msg_trap + 8
1  libsystem_kernel.dylib         0x1999c93a8 mach_msg + 72
2  CoreFoundation                 0x199dcdfb0 __CFRunLoopServiceMachPort + 236
3  CoreFoundation                 0x199dc8e4c __CFRunLoopRun + 1396
4  CoreFoundation                 0x199dc85b8 CFRunLoopRunSpecific + 436
5  Foundation                     0x19a7bc6a4 -[NSRunLoop(NSRunLoop) runMode:beforeDate:] + 300
6  MyApp                0x101212130 -[_FSRRunLoopThread main] (FSRWebSocket.m:1839)
7  Foundation                     0x19a8ef3b0 __NSThread__start__ + 1040
8  libsystem_pthread.dylib        0x199a592fc _pthread_body + 128
9  libsystem_pthread.dylib        0x199a5925c _pthread_start + 48
10 libsystem_pthread.dylib        0x199a5cd08 thread_start + 4


#11. Thread
0  libsystem_kernel.dylib         0x1999d5b9c __workq_kernreturn + 8
1  libsystem_pthread.dylib        0x199a5a114 _pthread_wqthread + 348
2  libsystem_pthread.dylib        0x199a5cd00 start_wqthread + 4


#12. Thread
0  libsystem_kernel.dylib         0x1999d5b9c __workq_kernreturn + 8
1  libsystem_pthread.dylib        0x199a5a1d4 _pthread_wqthread + 540
2  libsystem_pthread.dylib        0x199a5cd00 start_wqthread + 4