iosxibnsunknownkeyexception

NSUnknownKeyException even though outlets are defined


I have a custom view with a corresponding xib in a framework. In interface builder, I've set the File's Owner and the view itself to the my subclass of UIView (TotalTokenCountView). I've also setup outlets for two labels in the custom view. However, when I use this custom view in an application, I get an NSUnknownKeyException exception.

I've tried setting the outlet vars to public to ensure that they can be connected outside of the framework, but that didn't work either.

Am I missing something here? Is there more setup I need to do for the outlets to connect?

public class TotalTokenCountView: UIView {

    @IBOutlet public weak var tokenLabel: UILabel!
    @IBOutlet public weak var tokenCountLabel: UILabel!

    public override func awakeFromNib() {
        tokenCountLabel.textColor = UIColor.deepPurple
        tokenLabel.textColor = UIColor.lavenderGrayMedium
    }

    static func create() -> TotalTokenCountView {
        return mybundle.loadNibNamed("TotalTokenCountView", owner: self, options: nil)!.first! as! TotalTokenCountView
    }
}

The code that creates the custom view:

public class TotalTokenCountBarButtonItem: UIBarButtonItem {

    public required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        commonInit()
    }

    private func commonInit() {
        self.customView = TotalTokenCountView.create()
    }
}

IB Screenshots: outlets

custom class

Exception:

*** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<MyFrameworkTarget.TotalTokenCountView 0x104ed9580> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key tokenCountLabel.'
*** First throw call stack:
(
    0   CoreFoundation                      0x000000010a0d31e6 __exceptionPreprocess + 294
    1   libobjc.A.dylib                     0x000000010550a031 objc_exception_throw + 48
    2   CoreFoundation                      0x000000010a0d30b9 -[NSException raise] + 9
    3   Foundation                          0x0000000104f2bb47 -[NSObject(NSKeyValueCoding) setValue:forKey:] + 292
    4   UIKit                               0x000000010622fe8a -[UIRuntimeOutletConnection connect] + 109
    5   CoreFoundation                      0x000000010a075e8d -[NSArray makeObjectsPerformSelector:] + 317
    6   UIKit                               0x000000010622e834 -[UINib instantiateWithOwner:options:] + 1856
    7   MyFrameworkTarget                   0x0000000104ed73bf _T012MyFrameworkTarget19TotalTokenCountViewC6createACyFZ + 431
    8   MyFrameworkTarget                   0x0000000104ed817b _T012MyFrameworkTarget28TotalTokenCountBarButtonItemC10commonInit33_82940FA39F221CEB8A8645FA6323AFBFLLyyF + 43
    9   MyFrameworkTarget                   0x0000000104ed80bc _T012MyFrameworkTarget28TotalTokenCountBarButtonItemCACSgSo7NSCoderC5coder_tcfc + 124
    10  MyFrameworkTarget                   0x0000000104ed813f _T012MyFrameworkTarget28TotalTokenCountBarButtonItemCACSgSo7NSCoderC5coder_tcfcTo + 47
    11  UIKit                               0x000000010622f725 -[UIClassSwapper initWithCoder:] + 246
    12  UIKit                               0x000000010648188c UINibDecoderDecodeObjectForValue + 704
    13  UIKit                               0x00000001064815bf -[UINibDecoder decodeObjectForKey:] + 246
    14  UIKit                               0x000000010671219b -[UINavigationItem initWithCoder:] + 715
    15  UIKit                               0x000000010648188c UINibDecoderDecodeObjectForValue + 704
    16  UIKit                               0x00000001064815bf -[UINibDecoder decodeObjectForKey:] + 246
    17  UIKit                               0x0000000105f44db1 -[UIViewController initWithCoder:] + 407
    18  UIKit                               0x0000000105fac885 -[UITabBarController initWithCoder:] + 65
    19  UIKit                               0x000000010648188c UINibDecoderDecodeObjectForValue + 704
    20  UIKit                               0x0000000106481a2a UINibDecoderDecodeObjectForValue + 1118
    21  UIKit                               0x00000001064815bf -[UINibDecoder decodeObjectForKey:] + 246
    22  UIKit                               0x0000000105f451a0 -[UIViewController initWithCoder:] + 1414
    23  UIKit                               0x0000000105f74dcc -[UINavigationController initWithCoder:] + 65
    24  UIKit                               0x000000010648188c UINibDecoderDecodeObjectForValue + 704
    25  UIKit                               0x00000001064815bf -[UINibDecoder decodeObjectForKey:] + 246
    26  UIKit                               0x000000010622f3f1 -[UIRuntimeConnection initWithCoder:] + 178
    27  UIKit                               0x000000010648188c UINibDecoderDecodeObjectForValue + 704
    28  UIKit                               0x0000000106481a2a UINibDecoderDecodeObjectForValue + 1118
    29  UIKit                               0x00000001064815bf -[UINibDecoder decodeObjectForKey:] + 246
    30  UIKit                               0x000000010622e5e2 -[UINib instantiateWithOwner:options:] + 1262
    31  UIKit                               0x00000001066bb406 -[UIStoryboard instantiateViewControllerWithIdentifier:] + 181
    32  UIKit                               0x0000000105da5862 -[UIApplication _loadMainStoryboardFileNamed:bundle:] + 112
    33  UIKit                               0x0000000105da5d33 -[UIApplication _loadMainInterfaceFile] + 272
    34  UIKit                               0x0000000105da44b9 -[UIApplication _runWithMainScene:transitionContext:completion:] + 1403
    35  UIKit                               0x0000000106166f7e __111-[__UICanvasLifecycleMonitor_Compatability _scheduleFirstCommitForScene:transition:firstActivation:completion:]_block_invoke + 866
    36  UIKit                               0x0000000106539a39 +[_UICanvas _enqueuePostSettingUpdateTransactionBlock:] + 153
    37  UIKit                               0x0000000106166bba -[__UICanvasLifecycleMonitor_Compatability _scheduleFirstCommitForScene:transition:firstActivation:completion:] + 236
    38  UIKit                               0x00000001061673db -[__UICanvasLifecycleMonitor_Compatability activateEventsOnly:withContext:completion:] + 675
    39  UIKit                               0x0000000106ad8614 __82-[_UIApplicationCanvas _transitionLifecycleStateWithTransitionContext:completion:]_block_invoke + 299
    40  UIKit                               0x0000000106ad84ae -[_UIApplicationCanvas _transitionLifecycleStateWithTransitionContext:completion:] + 433
    41  UIKit                               0x00000001067bc75d __125-[_UICanvasLifecycleSettingsDiffAction performActionsForCanvas:withUpdatedScene:settingsDiff:fromSettings:transitionContext:]_block_invoke + 221
    42  UIKit                               0x00000001069b74b7 _performActionsWithDelayForTransitionContext + 100
    43  UIKit                               0x00000001067bc627 -[_UICanvasLifecycleSettingsDiffAction performActionsForCanvas:withUpdatedScene:settingsDiff:fromSettings:transitionContext:] + 223
    44  UIKit                               0x00000001065390e0 -[_UICanvas scene:didUpdateWithDiff:transitionContext:completion:] + 392
    45  UIKit                               0x0000000105da2eac -[UIApplication workspace:didCreateScene:withTransitionContext:completion:] + 515
    46  UIKit                               0x0000000106375bcb -[UIApplicationSceneClientAgent scene:didInitializeWithEvent:completion:] + 361
    47  FrontBoardServices                  0x000000010d5752f3 -[FBSSceneImpl _didCreateWithTransitionContext:completion:] + 331
    48  FrontBoardServices                  0x000000010d57dcfa __56-[FBSWorkspace client:handleCreateScene:withCompletion:]_block_invoke_2 + 225
    49  libdispatch.dylib                   0x00000001089c97ec _dispatch_client_callout + 8
    50  libdispatch.dylib                   0x00000001089cedb8 _dispatch_block_invoke_direct + 592
    51  FrontBoardServices                  0x000000010d5a9470 __FBSSERIALQUEUE_IS_CALLING_OUT_TO_A_BLOCK__ + 24
    52  FrontBoardServices                  0x000000010d5a912e -[FBSSerialQueue _performNext] + 439
    53  FrontBoardServices                  0x000000010d5a968e -[FBSSerialQueue _performNextFromRunLoopSource] + 45
    54  CoreFoundation                      0x000000010a075bb1 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
    55  CoreFoundation                      0x000000010a05a4af __CFRunLoopDoSources0 + 271
    56  CoreFoundation                      0x000000010a059a6f __CFRunLoopRun + 1263
    57  CoreFoundation                      0x000000010a05930b CFRunLoopRunSpecific + 635
    58  GraphicsServices                    0x000000010cdc9a73 GSEventRunModal + 62
    59  UIKit                               0x0000000105da6057 UIApplicationMain + 159
    60  PinataApp                           0x0000000104be62c7 main + 55
    61  libdyld.dylib                       0x0000000108a46955 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

Solution

  • Remove the references (outlets) from File's Owner and add them to TotalTokenCountView.