How to Convert CGEvent to NSEvent in Swift? - swift

I'm trying to convert CGEvent to NSEvent using NSEvent(cgEvent: event) in order to get the characters of key press, but I get the error saying it's an "Invalid event." My code and the error message are below. Any help would b eappreciated. Thanks!
func myCGEventCallback(proxy : CGEventTapProxy, type : CGEventType, event : CGEvent, refcon : UnsafeMutableRawPointer?) -> Unmanaged<CGEvent>? {
if type == .keyDown {
if let other = NSEvent(cgEvent: event), let chars = other.characters {
debugPrint(chars)
}
}
return Unmanaged.passRetained(event)
}
2017-06-10 23:18:14.233831-0400 RemapEvent[54431:3082409] [default] Invalid event.
2017-06-10 23:18:14.233938-0400 RemapEvent[54431:3082409] *** Assertion failure in -[NSEvent characters], /Library/Caches/com.apple.xbs/Sources/AppKit/AppKit-1504.83.101/AppKit.subproj/NSEvent.m:2414
2017-06-10 23:18:14.234153-0400 RemapEvent[54431:3082409] [General] Invalid message sent to event "NSEvent: type=FlagsChanged loc=(580,464.336) time=116091.6 flags=0x40101 win=0x0 winNum=0 ctxt=0x0 keyCode=59"
2017-06-10 23:18:14.234965-0400 RemapEvent[54431:3082409] [General] (
0 CoreFoundation 0x00007fffa16c62cb __exceptionPreprocess + 171
1 libobjc.A.dylib 0x00007fffb64d148d objc_exception_throw + 48
2 CoreFoundation 0x00007fffa16cb042 +[NSException raise:format:arguments:] + 98
3 Foundation 0x00007fffa3113c80 -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 195
4 AppKit 0x00007fff9f2caa64 -[NSEvent characters] + 174
5 RemapEvent 0x0000000100003da9 _TFFC10RemapEvent14ViewController11viewDidLoadFT_T_L_17myCGEventCallbackFT5proxyVs13OpaquePointer4typeOSC11CGEventType5eventCSo7CGEvent6refconGSqSv__GSqGVs9UnmanagedS3___ + 473
6 RemapEvent 0x0000000100004877 _TToFFC10RemapEvent14ViewController11viewDidLoadFT_T_L_17myCGEventCallbackFT5proxyVs13OpaquePointer4typeOSC11CGEventType5eventCSo7CGEvent6refconGSqSv__GSqGVs9UnmanagedS3___ + 55
7 SkyLight 0x00007fffb32c0dca processDecodedEventRef + 204
8 SkyLight 0x00007fffb32c05b4 processEventTapData + 544
9 SkyLight 0x00007fffb319e6db _XPostEventTapData + 280
10 SkyLight 0x00007fffb32c033e eventTapMessageHandler + 137
11 CoreFoundation 0x00007fffa16451ed __CFMachPortPerform + 253
12 CoreFoundation 0x00007fffa16450d9 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 41
13 CoreFoundation 0x00007fffa1645051 __CFRunLoopDoSource1 + 465
14 CoreFoundation 0x00007fffa163ccc5 __CFRunLoopRun + 2389
15 CoreFoundation 0x00007fffa163c114 CFRunLoopRunSpecific + 420
16 HIToolbox 0x00007fffa0b9debc RunCurrentEventLoopInMode + 240
17 HIToolbox 0x00007fffa0b9dcf1 ReceiveNextEventCommon + 432
18 HIToolbox 0x00007fffa0b9db26 _BlockUntilNextEventMatchingListInModeWithFilter + 71
19 AppKit 0x00007fff9f136a54 _DPSNextEvent + 1120
20 AppKit 0x00007fff9f8b27ee -[NSApplication(NSEvent) _nextEventMatchingEventMask:untilDate:inMode:dequeue:] + 2796
21 AppKit 0x00007fff9f12b3db -[NSApplication run] + 926
22 AppKit 0x00007fff9f0f5e0e NSApplicationMain + 1237
23 RemapEvent 0x000000010000648d main + 13
24 libdyld.dylib 0x00007fffb6db6235 start + 1
)

Related

macCatalyst crash on UIScene in macOS 11 (Big Sur) [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I have a macCatalyst app that works perfectly on macOS 10.14, however on upgrading to macOS 11 (Big Sur) both I and my users are complaining that the app crashes on launch. I get an error message "Parameter was not of class UIScene" - do macCatalyst apps now require the use of scenes?
On further inspection, I see the following:
2020-11-28 01:48:40.327810+0000 AppName [13691:435322] *** Assertion failure in -[_UISceneMacHelperUtilities sceneIdentifierForUIScene:], _UISceneMacHelperUtilities.m:83
2020-11-28 01:48:40.336334+0000 AppName[13691:435322] [General] An uncaught exception was raised
2020-11-28 01:48:40.336423+0000 AppName[13691:435322] [General] Parameter was not of class UIScene.
2020-11-28 01:48:40.336545+0000 AppName[13691:435322] [General] (
0 CoreFoundation 0x00007fff204ae6af __exceptionPreprocess + 242
1 libobjc.A.dylib 0x00007fff201e63c9 objc_exception_throw + 48
2 CoreFoundation 0x00007fff204d7512 +[NSException raise:format:arguments:] + 88
3 Foundation 0x00007fff212906c9 -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 191
4 UIKitCore 0x00007fff44a7b2c6 -[_UISceneMacHelperUtilities sceneIdentifierForUIScene:] + 195
5 UIKitMacHelper 0x00007fff34045f74 __46-[UINSApplicationDelegate _setupKeyWindowSync]_block_invoke + 393
6 Foundation 0x00007fff21192d6b -[__NSObserver _doit:] + 287
7 CoreFoundation 0x00007fff20429fdc __CFNOTIFICATIONCENTER_IS_CALLING_OUT_TO_AN_OBSERVER__ + 12
8 CoreFoundation 0x00007fff204c589b ___CFXRegistrationPost_block_invoke + 49
9 CoreFoundation 0x00007fff204c580f _CFXRegistrationPost + 454
10 CoreFoundation 0x00007fff203fabce _CFXNotificationPost + 723
11 Foundation 0x00007fff21166abe -[NSNotificationCenter postNotificationName:object:userInfo:] + 59
12 UIKitCore 0x00007fff44376779 -[UIWindow _makeKeyWindowIgnoringOldKeyWindow:] + 567
13 AppName 0x00000001013fe0c0 $s25AppName13SceneDelegateC5scene_13willConnectTo7optionsySo7UISceneC_So0K7SessionCSo0K17ConnectionOptionsCtF + 1520
14 AppName 0x00000001013feaa6 $s25AppName13SceneDelegateC5scene_13willConnectTo7optionsySo7UISceneC_So0K7SessionCSo0K17ConnectionOptionsCtFTo + 118
15 UIKitCore 0x00007fff44221a31 +[UIScene _sceneForFBSScene:create:withSession:connectionOptions:] + 1463
16 UIKitCore 0x00007fff4421ffb1 -[UIApplication _connectUISceneFromFBSScene:transitionContext:] + 1109
17 UIKitCore 0x00007fff4421ecfe -[UIApplication workspace:didCreateScene:withTransitionContext:completion:] + 299
18 UIKitCore 0x00007fff4421eb49 -[UIApplicationSceneClientAgent scene:didInitializeWithEvent:completion:] + 358
19 FrontBoardServices 0x00007fff317e7312 -[FBSScene _callOutQueue_agent_didCreateWithTransitionContext:completion:] + 391
20 FrontBoardServices 0x00007fff318045df __94-[FBSWorkspaceScenesClient createWithSceneID:groupID:parameters:transitionContext:completion:]_block_invoke.176 + 102
21 FrontBoardServices 0x00007fff317d60c1 -[FBSWorkspace _calloutQueue_executeCalloutFromSource:withBlock:] + 209
22 FrontBoardServices 0x00007fff318042b7 __94-[FBSWorkspaceScenesClient createWithSceneID:groupID:parameters:transitionContext:completion:]_block_invoke + 354
23 libdispatch.dylib 0x00000001040c50b0 _dispatch_client_callout + 8
24 libdispatch.dylib 0x00000001040c8927 _dispatch_block_invoke_direct + 286
25 FrontBoardServices 0x00007fff317d5fd8 __FBSSERIALQUEUE_IS_CALLING_OUT_TO_A_BLOCK__ + 30
26 FrontBoardServices 0x00007fff3181e1c3 -[FBSSerialQueue _targetQueue_performNextIfPossible] + 433
27 FrontBoardServices 0x00007fff317d5fa0 -[FBSSerialQueue _performNextFromRunLoopSource] + 22
28 CoreFoundation 0x00007fff204349fc __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
29 CoreFoundation 0x00007fff20434964 __CFRunLoopDoSource0 + 180
30 CoreFoundation 0x00007fff204346df __CFRunLoopDoSources0 + 248
31 CoreFoundation 0x00007fff20433111 __CFRunLoopRun + 890
32 CoreFoundation 0x00007fff204326be CFRunLoopRunSpecific + 563
33 HIToolbox 0x00007fff2869efd0 RunCurrentEventLoopInMode + 292
34 HIToolbox 0x00007fff2869edcc ReceiveNextEventCommon + 709
35 HIToolbox 0x00007fff2869eaef _BlockUntilNextEventMatchingListInModeWithFilter + 64
36 AppKit 0x00007fff22c4bf85 _DPSNextEvent + 883
37 AppKit 0x00007fff22c4a74b -[NSApplication(NSEvent) _nextEventMatchingEventMask:untilDate:inMode:dequeue:] + 1366
38 AppKit 0x00007fff22c3cbda -[NSApplication run] + 586
39 AppKit 0x00007fff22c10f31 NSApplicationMain + 816
40 AppKit 0x00007fff22f0744d _NSApplicationMainWithInfoDictionary + 16
41 UIKitMacHelper 0x00007fff3403332e UINSApplicationMain + 1418
42 UIKitCore 0x00007fff44202e30 UIApplicationMain + 144
43 AppName 0x00000001014da40b main + 75
44 libdyld.dylib 0x00007fff20357631 start + 1
)
2020-11-28 01:48:40.337802+0000 AppName[13691:435322] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Parameter was not of class UIScene.'
*** First throw call stack:
(
0 CoreFoundation 0x00007fff204ae6af __exceptionPreprocess + 242
1 libobjc.A.dylib 0x00007fff201e63c9 objc_exception_throw + 48
2 CoreFoundation 0x00007fff204d7512 +[NSException raise:format:arguments:] + 88
3 Foundation 0x00007fff212906c9 -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 191
4 UIKitCore 0x00007fff44a7b2c6 -[_UISceneMacHelperUtilities sceneIdentifierForUIScene:] + 195
5 UIKitMacHelper 0x00007fff34045f74 __46-[UINSApplicationDelegate _setupKeyWindowSync]_block_invoke + 393
6 Foundation 0x00007fff21192d6b -[__NSObserver _doit:] + 287
7 CoreFoundation 0x00007fff20429fdc __CFNOTIFICATIONCENTER_IS_CALLING_OUT_TO_AN_OBSERVER__ + 12
8 CoreFoundation 0x00007fff204c589b ___CFXRegistrationPost_block_invoke + 49
9 CoreFoundation 0x00007fff204c580f _CFXRegistrationPost + 454
10 CoreFoundation 0x00007fff203fabce _CFXNotificationPost + 723
11 Foundation 0x00007fff21166abe -[NSNotificationCenter postNotificationName:object:userInfo:] + 59
12 UIKitCore 0x00007fff44376779 -[UIWindow _makeKeyWindowIgnoringOldKeyWindow:] + 567
13 AppName 0x00000001013fe0c0 $s25AppName13SceneDelegateC5scene_13willConnectTo7optionsySo7UISceneC_So0K7SessionCSo0K17ConnectionOptionsCtF + 1520
14 AppName 0x00000001013feaa6 $s25AppName13SceneDelegateC5scene_13willConnectTo7optionsySo7UISceneC_So0K7SessionCSo0K17ConnectionOptionsCtFTo + 118
15 UIKitCore 0x00007fff44221a31 +[UIScene _sceneForFBSScene:create:withSession:connectionOptions:] + 1463
16 UIKitCore 0x00007fff4421ffb1 -[UIApplication _connectUISceneFromFBSScene:transitionContext:] + 1109
17 UIKitCore 0x00007fff4421ecfe -[UIApplication workspace:didCreateScene:withTransitionContext:completion:] + 299
18 UIKitCore 0x00007fff4421eb49 -[UIApplicationSceneClientAgent scene:didInitializeWithEvent:completion:] + 358
19 FrontBoardServices 0x00007fff317e7312 -[FBSScene _callOutQueue_agent_didCreateWithTransitionContext:completion:] + 391
20 FrontBoardServices 0x00007fff318045df __94-[FBSWorkspaceScenesClient createWithSceneID:groupID:parameters:transitionContext:completion:]_block_invoke.176 + 102
21 FrontBoardServices 0x00007fff317d60c1 -[FBSWorkspace _calloutQueue_executeCalloutFromSource:withBlock:] + 209
22 FrontBoardServices 0x00007fff318042b7 __94-[FBSWorkspaceScenesClient createWithSceneID:groupID:parameters:transitionContext:completion:]_block_invoke + 354
23 libdispatch.dylib 0x00000001040c50b0 _dispatch_client_callout + 8
24 libdispatch.dylib 0x00000001040c8927 _dispatch_block_invoke_direct + 286
25 FrontBoardServices 0x00007fff317d5fd8 __FBSSERIALQUEUE_IS_CALLING_OUT_TO_A_BLOCK__ + 30
26 FrontBoardServices 0x00007fff3181e1c3 -[FBSSerialQueue _targetQueue_performNextIfPossible] + 433
27 FrontBoardServices 0x00007fff317d5fa0 -[FBSSerialQueue _performNextFromRunLoopSource] + 22
28 CoreFoundation 0x00007fff204349fc __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
29 CoreFoundation 0x00007fff20434964 __CFRunLoopDoSource0 + 180
30 CoreFoundation 0x00007fff204346df __CFRunLoopDoSources0 + 248
31 CoreFoundation 0x00007fff20433111 __CFRunLoopRun + 890
32 CoreFoundation 0x00007fff204326be CFRunLoopRunSpecific + 563
33 HIToolbox 0x00007fff2869efd0 RunCurrentEventLoopInMode + 292
34 HIToolbox 0x00007fff2869edcc ReceiveNextEventCommon + 709
35 HIToolbox 0x00007fff2869eaef _BlockUntilNextEventMatchingListInModeWithFilter + 64
36 AppKit 0x00007fff22c4bf85 _DPSNextEvent + 883
37 AppKit 0x00007fff22c4a74b -[NSApplication(NSEvent) _nextEventMatchingEventMask:untilDate:inMode:dequeue:] + 1366
38 AppKit 0x00007fff22c3cbda -[NSApplication run] + 586
39 AppKit 0x00007fff22c10f31 NSApplicationMain + 816
40 AppKit 0x00007fff22f0744d _NSApplicationMainWithInfoDictionary + 16
41 UIKitMacHelper 0x00007fff3403332e UINSApplicationMain + 1418
42 UIKitCore 0x00007fff44202e30 UIApplicationMain + 144
43 AppName 0x00000001014da40b main + 75
44 libdyld.dylib 0x00007fff20357631 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Parameter was not of class UIScene.'
terminating with uncaught exception of type NSException

Allow user customization of Touch Bar in macCatalyst

In a catalyst Swift project a UIViewController subclass creates a TouchBar like this:
override func makeTouchBar() -> NSTouchBar? {
let touchBar = NSTouchBar()
touchBar.delegate = self
touchBar.defaultItemIdentifiers = [<<some>>]
touchBar.customizationAllowedItemIdentifiers = [<<more>>]
touchBar.customizationIdentifier = "com.me.myapp.touchbar"
return touchBar
}
The default items show up on the TouchBar, but there is no menu item to customize them.
Reading the TouchBar documentation there are two options:
1: NSApplication
The Customization Menu Item
A user invokes the customization UI for a particular NSTouchBar object, when it is visible in the Touch Bar, by choosing the bar customization menu item. To enable this menu item you must explicitly opt-in, which you can do in the following ways:
If you want the system to automatically name, place, validate, and activate this menu item in your app’s menus, set the isAutomaticCustomizeTouchBarMenuItemEnabled property of your app object (of type NSApplication) to true.
To explicitly place the customization menu item in one of your app’s menus, employ the toggleTouchBarCustomizationPalette(_:) method of your app object. When you do this, the system still names and validates the menu item, and hides it on systems that do not have a Touch Bar.
Trying to do either of those yields a 'NSApplication' is unavailable in Mac Catalyst error.
So after some googling around I found this:
NSClassFromString("NSApplication")?.setValue(true, forKeyPath: "sharedApplication.automaticCustomizeTouchBarMenuItemEnabled")
print(NSClassFromString("NSApplication")?.value(forKeyPath: "sharedApplication.isAutomaticCustomizeTouchBarMenuItemEnabled"))
This compiles and executes fine. It even prints out Optional(1) where there was an Optional(0) before. But the menu item still is not there. I tried this at the point where I generate the TouchBar, where I generate the Menu, and in func application(_, didFinishLaunchingWithOptions), but to no avail.
I cannot get the second one to not give me an error.
2: TouchBar
Curiously the NSTouchBar has the same property, but running
NSTouchBar.isAutomaticCustomizeTouchBarMenuItemEnabled = true
at any point (which compiles, too) yields no difference.
Question
Is this a missing feature or is there a way to achieve it. And if so: how?
Edit
I am customizing the menu bar. Due to an error, setting the same shortcut for two options, causes the following output in Xcode:
2020-02-15 14:08:01.492510+0100 Myapp[71888:2189305] [MenuBuilder] Menu has duplicates --
<_UIImmutableKeyCommand: 0x60000374e700> -> Title: Toggle Center Column Action: executeOptionForKeyCommand: Input: 1 + (UIKeyModifierCommand)
<_UIImmutableKeyCommand: 0x60000374e640> -> Title: Toggle Left Column Action: executeOptionForKeyCommand: Input: 1 + (UIKeyModifierCommand)
Make a symbolic breakpoint at _UIMenuBuilderError to catch this in the debugger.
2020-02-15 14:08:01.492701+0100 Myapp[71888:2189305] *** Assertion failure in -[_UIMenuBuilder _insertMenu:intoParentMenu:newParentMenu:], /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKitCore/UIKit-3901.4.905/UIMenu/UIMenuBuilder.m:456
2020-02-15 14:08:01.492950+0100 Myapp[71888:2189305] [General] inserted menu has duplicate submenu, command or key command, or a key command is missing input or action
2020-02-15 14:08:01.497025+0100 Myapp[71888:2189305] [General] (
0 CoreFoundation 0x00007fff2f3858ab __exceptionPreprocess + 250
1 libobjc.A.dylib 0x00007fff6563f805 objc_exception_throw + 48
2 CoreFoundation 0x00007fff2f3aed10 +[NSException raise:format:arguments:] + 88
3 Foundation 0x00007fff31aa7241 -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 191
4 UIKitCore 0x00007fff6d31dded -[_UIMenuBuilder _insertMenu:intoParentMenu:newParentMenu:] + 1426
5 UIKitCore 0x00007fff6d31e9ef -[_UIMenuBuilder insertChildMenu:atStartOfMenuForIdentifier:] + 177
6 Myapp 0x000000010010a87b $s5Myapp11AppDelegateC9buildMenu4withySo13UIMenuBuilder_p_tF + 4987
7 Myapp 0x000000010010b150 $s5Myapp11AppDelegateC9buildMenu4withySo13UIMenuBuilder_p_tFTo + 64
8 UIKitCore 0x00007fff6d31c428 -[UIResponder _buildMenuFromChainWithBuilder:] + 115
9 UIKitCore 0x00007fff6d31c17f -[UIMenuSystem _newBuilderFromResponderChain:] + 72
10 UIKitCore 0x00007fff6d31c114 -[UIMenuSystem _automaticallyRebuildIfNeeded] + 88
11 UIKitCore 0x00007fff6d31c09e -[UIMenuSystem _rootMenu] + 27
12 UIKitCore 0x00007fff6d3144d9 -[_UIMenuBarController _rebuildRootCommandGroup] + 66
13 Foundation 0x00007fff3197ffab -[__NSObserver _doit:] + 296
14 CoreFoundation 0x00007fff2f2ff35f __CFNOTIFICATIONCENTER_IS_CALLING_OUT_TO_AN_OBSERVER__ + 12
15 CoreFoundation 0x00007fff2f2ff2f3 ___CFXRegistrationPost1_block_invoke + 63
16 CoreFoundation 0x00007fff2f2ff268 _CFXRegistrationPost1 + 372
17 CoreFoundation 0x00007fff2f2feebe ___CFXNotificationPost_block_invoke + 97
18 CoreFoundation 0x00007fff2f2ce7e2 -[_CFXNotificationRegistrar find:object:observer:enumerator:] + 1575
19 CoreFoundation 0x00007fff2f2cdc82 _CFXNotificationPost + 1351
20 Foundation 0x00007fff319e6048 postQueueNotifications + 718
21 CoreFoundation 0x00007fff2f3090ee __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23
22 CoreFoundation 0x00007fff2f309014 __CFRunLoopDoObservers + 457
23 CoreFoundation 0x00007fff2f308832 __CFRunLoopRun + 1514
24 CoreFoundation 0x00007fff2f307bd3 CFRunLoopRunSpecific + 499
25 HIToolbox 0x00007fff2de5d65d RunCurrentEventLoopInMode + 292
26 HIToolbox 0x00007fff2de5d39d ReceiveNextEventCommon + 600
27 HIToolbox 0x00007fff2de5d127 _BlockUntilNextEventMatchingListInModeWithFilter + 64
28 AppKit 0x00007fff2c4cdba4 _DPSNextEvent + 990
29 AppKit 0x00007fff2c4cc380 -[NSApplication(NSEvent) _nextEventMatchingEventMask:untilDate:inMode:dequeue:] + 1352
30 AppKit 0x00007fff2c4be09e -[NSApplication run] + 658
31 AppKit 0x00007fff2c490465 NSApplicationMain + 777
32 AppKit 0x00007fff2c7b369c _NSApplicationMainWithInfoDictionary + 16
33 UIKitMacHelper 0x00007fff602de8f1 UINSApplicationMain + 322
34 UIKitCore 0x00007fff6d186273 UIApplicationMain + 2105
35 Myapp 0x000000010010b39b main + 75
36 libdyld.dylib 0x00007fff669ad7fd start + 1
)
2020-02-15 14:08:27.979587+0100 Myapp[71888:2189305] [Layout] Unable to simultaneously satisfy constraints:
(
"<NSAutoresizingMaskLayoutConstraint:0x600002183ac0 h=-&- v=-&- _SC_RESULTS_TABLE.minY == 0 (active, names: _SC_RESULTS_TABLE:0x100dc3140, '|':SCTMenuView:0x100dca2e0 )>",
"<NSAutoresizingMaskLayoutConstraint:0x600002183f70 h=-&- v=-&- V:|-(202)-[_SC_RESULTS_TABLE] (active, names: _SC_RESULTS_TABLE:0x100dc3140, '|':SCTMenuView:0x100dca2e0 )>",
"<NSAutoresizingMaskLayoutConstraint:0x60000217f200 h=--& v=--& SCTMenuView:0x100dca2e0.height == 59 (active)>"
)
Will attempt to recover by breaking constraint
<NSAutoresizingMaskLayoutConstraint:0x600002183f70 h=-&- v=-&- V:|-(202)-[_SC_RESULTS_TABLE] (active, names: _SC_RESULTS_TABLE:0x100dc3140, '|':SCTMenuView:0x100dca2e0 )>
Set the NSUserDefault NSConstraintBasedLayoutVisualizeMutuallyExclusiveConstraints to YES to have -[NSWindow visualizeConstraints:] automatically called when this happens. And/or, set a symbolic breakpoint on LAYOUT_CONSTRAINTS_NOT_SATISFIABLE to catch this in the debugger.
2020-02-15 14:08:29.504823+0100 Myapp[71888:2189305] [MenuBuilder] Menu has duplicates --
<_UIImmutableKeyCommand: 0x60000375e400> -> Title: Toggle Center Column Action: executeOptionForKeyCommand: Input: 1 + (UIKeyModifierCommand)
<_UIImmutableKeyCommand: 0x60000375e340> -> Title: Toggle Left Column Action: executeOptionForKeyCommand: Input: 1 + (UIKeyModifierCommand)
Make a symbolic breakpoint at _UIMenuBuilderError to catch this in the debugger.
2020-02-15 14:08:29.504936+0100 Myapp[71888:2189305] *** Assertion failure in -[_UIMenuBuilder _insertMenu:intoParentMenu:newParentMenu:], /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKitCore/UIKit-3901.4.905/UIMenu/UIMenuBuilder.m:456
2020-02-15 14:08:29.506015+0100 Myapp[71888:2189305] [General] inserted menu has duplicate submenu, command or key command, or a key command is missing input or action
2020-02-15 14:08:29.514279+0100 Myapp[71888:2189305] [General] (
0 CoreFoundation 0x00007fff2f3858ab __exceptionPreprocess + 250
1 libobjc.A.dylib 0x00007fff6563f805 objc_exception_throw + 48
2 CoreFoundation 0x00007fff2f3aed10 +[NSException raise:format:arguments:] + 88
3 Foundation 0x00007fff31aa7241 -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 191
4 UIKitCore 0x00007fff6d31dded -[_UIMenuBuilder _insertMenu:intoParentMenu:newParentMenu:] + 1426
5 UIKitCore 0x00007fff6d31e9ef -[_UIMenuBuilder insertChildMenu:atStartOfMenuForIdentifier:] + 177
6 Myapp 0x000000010010a87b $s5Myapp11AppDelegateC9buildMenu4withySo13UIMenuBuilder_p_tF + 4987
7 Myapp 0x000000010010b150 $s5Myapp11AppDelegateC9buildMenu4withySo13UIMenuBuilder_p_tFTo + 64
8 UIKitCore 0x00007fff6d31c428 -[UIResponder _buildMenuFromChainWithBuilder:] + 115
9 UIKitCore 0x00007fff6d31c17f -[UIMenuSystem _newBuilderFromResponderChain:] + 72
10 UIKitCore 0x00007fff6d31c114 -[UIMenuSystem _automaticallyRebuildIfNeeded] + 88
11 UIKitCore 0x00007fff6d3cf5aa -[UIMenuSystem _keyCommands] + 27
12 UIKitCore 0x00007fff6d3cf375 -[UIApplication _keyCommands] + 105
13 UIKitCore 0x00007fff6dd17069 -[UIResponder _keyCommandForEvent:target:] + 182
14 UIKitCore 0x00007fff6dd17405 -[UIResponder _keyCommandForEvent:target:] + 1106
15 UIKitCore 0x00007fff6dd17405 -[UIResponder _keyCommandForEvent:target:] + 1106
16 UIKitCore 0x00007fff6dd17405 -[UIResponder _keyCommandForEvent:target:] + 1106
17 UIKitCore 0x00007fff6dd17405 -[UIResponder _keyCommandForEvent:target:] + 1106
18 UIKitCore 0x00007fff6dd17405 -[UIResponder _keyCommandForEvent:target:] + 1106
19 UIKitCore 0x00007fff6e0a36ce -[UIApplication(iOSMacSupport) _physicalKeyEvent:unmodified:shiftModified:commandModified:modifierFlags:isDown:timestampMachAbs:] + 517
20 UIKitCore 0x00007fff6e0a3803 -[UIApplication(iOSMacSupport) _sendKeyEvent:unmodified:shiftModified:commandModified:modifierFlags:isDown:timestampMachAbs:] + 116
21 UIKitCore 0x00007fff6e0a1c38 __58-[UIApplication(iOSMacSupport) _initiateIOSMacConnections]_block_invoke_3.396 + 124
22 UIKitMacHelper 0x00007fff602f4e3c -[UINSInputView _sendKeyEvent:isDown:] + 452
23 UIKitMacHelper 0x00007fff602f4ebd -[UINSInputView keyUp:] + 47
24 AppKit 0x00007fff2c675b98 -[NSWindow(NSEventRouting) _reallySendEvent:isDelayedEvent:] + 6512
25 AppKit 0x00007fff2c674005 -[NSWindow(NSEventRouting) sendEvent:] + 349
26 AppKit 0x00007fff2c672ccf -[NSApplication(NSEvent) sendEvent:] + 2739
27 AppKit 0x00007fff2c4be0cf -[NSApplication run] + 707
28 AppKit 0x00007fff2c490465 NSApplicationMain + 777
29 AppKit 0x00007fff2c7b369c _NSApplicationMainWithInfoDictionary + 16
30 UIKitMacHelper 0x00007fff602de8f1 UINSApplicationMain + 322
31 UIKitCore 0x00007fff6d186273 UIApplicationMain + 2105
32 Myapp 0x000000010010b39b main + 75
33 libdyld.dylib 0x00007fff669ad7fd start + 1
)
2020-02-15 14:08:30.338067+0100 Myapp[71888:2189305] *** Assertion failure in -[NSApplication _commonBeginModalSessionForWindow:relativeToWindow:modalDelegate:didEndSelector:contextInfo:], /BuildRoot/Library/Caches/com.apple.xbs/Sources/AppKit/AppKit-1894.30.142/AppKit.subproj/NSApplication.m:3694
2020-02-15 14:08:30.338318+0100 Myapp[71888:2189305] [General] Modal session requires modal window
2020-02-15 14:08:30.348833+0100 Myapp[71888:2189305] [General] (
0 CoreFoundation 0x00007fff2f3858ab __exceptionPreprocess + 250
1 libobjc.A.dylib 0x00007fff6563f805 objc_exception_throw + 48
2 CoreFoundation 0x00007fff2f3aed10 +[NSException raise:format:arguments:] + 88
3 Foundation 0x00007fff31aa7241 -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 191
4 AppKit 0x00007fff2c7cf9d0 -[NSApplication _commonBeginModalSessionForWindow:relativeToWindow:modalDelegate:didEndSelector:contextInfo:] + 1583
5 AppKit 0x00007fff2c7cf39b -[NSApplication beginModalSessionForWindow:] + 37
6 AppKit 0x00007fff2c7cf2fc __35-[NSApplication runModalForWindow:]_block_invoke_2 + 39
7 AppKit 0x00007fff2c7cf2c2 __35-[NSApplication runModalForWindow:]_block_invoke + 70
8 AppKit 0x00007fff2c7ceb4c _NSTryRunModal + 100
9 AppKit 0x00007fff2c7cea31 -[NSApplication runModalForWindow:] + 128
10 AppKit 0x00007fff2cd21be6 __82-[NSTouchBarCustomizationController toggleCustomizationPalette:forceControlStrip:]_block_invoke_4 + 148
11 AppKit 0x00007fff2cd244c7 ___NSRunLoopTimerCreateWithHandler_block_invoke + 34
12 CoreFoundation 0x00007fff2f324804 __CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION__ + 20
13 CoreFoundation 0x00007fff2f3243be __CFRunLoopDoTimer + 859
14 CoreFoundation 0x00007fff2f323e9e __CFRunLoopDoTimers + 317
15 CoreFoundation 0x00007fff2f308aed __CFRunLoopRun + 2213
16 CoreFoundation 0x00007fff2f307bd3 CFRunLoopRunSpecific + 499
17 HIToolbox 0x00007fff2de5d65d RunCurrentEventLoopInMode + 292
18 HIToolbox 0x00007fff2de5d2a9 ReceiveNextEventCommon + 356
19 HIToolbox 0x00007fff2de5d127 _BlockUntilNextEventMatchingListInModeWithFilter + 64
20 AppKit 0x00007fff2c4cdba4 _DPSNextEvent + 990
21 AppKit 0x00007fff2c4cc380 -[NSApplication(NSEvent) _nextEventMatchingEventMask:untilDate:inMode:dequeue:] + 1352
22 AppKit 0x00007fff2c4be09e -[NSApplication run] + 658
23 AppKit 0x00007fff2c490465 NSApplicationMain + 777
24 AppKit 0x00007fff2c7b369c _NSApplicationMainWithInfoDictionary + 16
25 UIKitMacHelper 0x00007fff602de8f1 UINSApplicationMain + 322
26 UIKitCore 0x00007fff6d186273 UIApplicationMain + 2105
27 Myapp 0x000000010010b39b main + 75
28 libdyld.dylib 0x00007fff669ad7fd start + 1
)
2020-02-15 14:08:31.815095+0100 Myapp[71888:2189305] [AXRuntimeCommon] Unknown client: Myapp
But then the customize TouchBar action shows up in the menu (all my custom ones are gone), the ❗MISSING LABEL❗ issue is easily fixed.
So I wondered whether it was my menu customization code causing the error, but uncommenting the entire override func buildMenu(with builder: UIMenuBuilder) in my AppDelegate does not cause the customization menu to appear – only the shortcut collision does.
In my Swift Catalyst app (macOS 10.15.3, Xcode 11.3.1), I am able to call
NSTouchBar.isAutomaticCustomizeTouchBarMenuItemEnabled = true
in my override func makeTouchBar() method, and the "Customize Touch Bar..." menu item appears in the "View" menu tab. For the missing labels, the following works:
func touchBar(_ touchBar: NSTouchBar, makeItemForIdentifier identifier: NSTouchBarItem.Identifier) -> NSTouchBarItem? {
if identifier == yourIdentifier {
let item = NSPickerTouchBarItem(...)
item.customizationLabel = "View Segmented Control"
return item
}
return nil
}

Using Cocoa NSSavePanel in Sandbox causes Assertion failure

I'm trying to use the NSSavePanel and added this line to my code.
let test = NSSavePanel()
Everytime this code is called the this error appears.
I'm not quite sure what is happening here because I'm only creating a new object.
Any help appreciated.
Thanks!
*** Assertion failure in -[NSVBSavePanel viewWillInvalidate:], /BuildRoot/Library/Caches/com.apple.xbs/Sources/AppKit/AppKit-1561.20.106/Nav.subproj/OpenAndSavePanelRemote/NSVBOpenAndSavePanels.m:387
2017-12-23 18:11:33.110099+0100 test[27753:1527254] -[NSVBSavePanel init] caught non-fatal NSInternalInconsistencyException 'bridge absent' with backtrace (
0 CoreFoundation 0x00007fff3ca1900b __exceptionPreprocess + 171
1 libobjc.A.dylib 0x00007fff635f7c76 objc_exception_throw + 48
2 CoreFoundation 0x00007fff3ca1eda2 +[NSException raise:format:arguments:] + 98
3 Foundation 0x00007fff3eb2b260 -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 193
4 AppKit 0x00007fff3a1e748e -[NSVBSavePanel viewWillInvalidate:] + 188
5 ViewBridge 0x00007fff60f376f2 -[NSRemoteView invalidate:] + 292
6 ViewBridge 0x00007fff60f44e88 -[NSRemoteView _advanceToConfigPhaseLegacy] + 1111
7 ViewBridge 0x00007fff60f45d1b -[NSRemoteView _viewServiceMarshalProxy:withDetailedErrorHandler:] + 230
8 ViewBridge 0x00007fff60f46104 -[NSRemoteView _viewServiceMarshalProxy:withErrorHandler:] + 78
9 ViewBridge 0x00007fff60f38194 -[NSRemoteView bridge] + 227
10 AppKit 0x00007fff3a1a2e36 -[NSVBSavePanel init] + 292
11 AppKit 0x00007fff3a66fc7e -[NSSavePanel initWithContentRect:styleMask:backing:defer:] + 71
12 AppKit 0x00007fff3a1a3093 -[NSPanel init] + 87
13 test 0x0000000100002933 _T0So11NSSavePanelCABycfcTO + 19
14 test 0x000000010000131c _T0So11NSSavePanelCABycfC + 60
15 test 0x0000000100001285 _T04test14ViewControllerC11viewDidLoadyyF + 85
16 test 0x00000001000013b4 _T04test14ViewControllerC11viewDidLoadyyFTo + 36
17 AppKit 0x00007fff39fc1632 -[NSViewController _sendViewDidLoad] + 97
18 AppKit 0x00007fff39fb806d -[NSViewController _loadViewIfRequired] + 390
19 AppKit 0x00007fff39fb7e9d -[NSViewController view] + 30
20 AppKit 0x00007fff3a13675d -[NSWindow _contentViewControllerChanged] + 109
21 Foundation 0x00007fff3ea99ade -[NSObject(NSKeyValueCoding) setValue:forKey:] + 331
22 AppKit 0x00007fff3a1794b1 -[NSWindow setValue:forKey:] + 111
23 AppKit 0x00007fff3a179415 -[NSIBUserDefinedRuntimeAttributesConnector establishConnection] + 637
24 AppKit 0x00007fff39f45bbd -[NSIBObjectData nibInstantiateWithOwner:options:topLevelObjects:] + 1430
25 AppKit 0x00007fff3a04113a -[NSNib _instantiateNibWithExternalNameTable:options:] + 679
26 AppKit 0x00007fff3a040d96 -[NSNib _instantiateWithOwner:options:topLevelObjects:] + 136
27 AppKit 0x00007fff3a797180 -[NSStoryboard instantiateControllerWithIdentifier:] + 236
28 AppKit 0x00007fff39f39ecf NSApplicationMain + 729
29 test 0x0000000100002d0d main + 13
30 libdyld.dylib 0x00007fff641e7115 start + 1
)
Your File Access permissions for the User Selected File type are probably still set to the default Read Only value
Navigate to the 'Capabilities' tab for your target
Set the User Selected File permission to Read/Write
Run your app (the bridge absent error should be gone)

caught non-fatal NSInvalidArgumentExecption in -[NSScrollView reloadData]

I have built a document based app in swift and then I added a NSCollectionView to it with an add button next to it. The button would call a NSOpenPanel and then the icon of the item chosen should appear in the collection view. However, when I click the 'open' button in the NSOpenPanel, the following were printed to the console:
2017-01-30 19:36:54.205343 CookRecipes[5004:203650] -[NSScrollView reloadData]: unrecognized selector sent to instance 0x6180001c1770
2017-01-30 19:36:54.224746 CookRecipes[5004:203650] -[NSVBSavePanel didEndPanelWithReturnCode:] caught non-fatal NSInvalidArgumentException '-[NSScrollView reloadData]: unrecognized selector sent to instance 0x6180001c1770' with backtrace (
0 CoreFoundation 0x00007fff9e2010db __exceptionPreprocess + 171
1 libobjc.A.dylib 0x00007fffb2e92a2a objc_exception_throw + 48
2 CoreFoundation 0x00007fff9e281cd4 -[NSObject(NSObject) doesNotRecognizeSelector:] + 132
3 CoreFoundation 0x00007fff9e1742a3 ___forwarding___ + 1059
4 CoreFoundation 0x00007fff9e173df8 _CF_forwarding_prep_0 + 120
5 CookRecipes 0x0000000100004239 _TFFC11CookRecipes8Document7addFileFT_T_U_FSiT_ + 457
6 CookRecipes 0x00000001000047ef _TTRXFo_dSi__XFdCb_dSi__ + 47
7 AppKit 0x00007fff9c0665e4 -[NSVBSavePanel didEndPanelWithReturnCode:] + 68
8 AppKit 0x00007fff9c6ba09d -[NSVBSavePanel completeModeless:] + 74
9 AppKit 0x00007fff9befa721 -[NSVBSavePanel observeValueForKeyPath:ofObject:change:context:] + 597
10 Foundation 0x00007fff9fbcb71d NSKeyValueNotifyObserver + 350
11 Foundation 0x00007fff9fbcafb4 NSKeyValueDidChange + 486
12 Foundation 0x00007fff9fb99e96 -[NSObject(NSKeyValueObserverNotification) didChangeValueForKey:] + 118
13 ViewBridge 0x00007fffb184c066 __41-[NSViewBridge setObject:forKey:withKVO:]_block_invoke + 447
14 ViewBridge 0x00007fffb18d052b withHintInProgress + 487
15 ViewBridge 0x00007fffb184be83 -[NSViewBridge setObject:forKey:withKVO:] + 1014
16 ViewBridge 0x00007fffb184a95a -[NSViewBridge nonLocalChangeInProgress:block:] + 390
17 ViewBridge 0x00007fffb18aa260 -[NSRemoteViewMarshal exceptionSafeSetRemoteObject:forKey:withReply:] + 256
18 ViewBridge 0x00007fffb18aa3e8 -[NSRemoteViewMarshal setRemoteObject:forKey:withReply:] + 56
19 CoreFoundation 0x00007fff9e1756dc __invoking___ + 140
20 CoreFoundation 0x00007fff9e175561 -[NSInvocation invoke] + 289
21 ViewBridge 0x00007fffb18d052b withHintInProgress + 487
22 ViewBridge 0x00007fffb18d025a __deferNSXPCInvocationOntoMainThread_block_invoke_2 + 233
23 ViewBridge 0x00007fffb1851df2 +[NSViewServiceApplication withHostPID:invoke:] + 46
24 ViewBridge 0x00007fffb18d011d __deferNSXPCInvocationOntoMainThread_block_invoke + 179
25 ViewBridge 0x00007fffb18cfd0a __deferBlockOntoMainThread_block_invoke_2 + 544
26 CoreFoundation 0x00007fff9e19771c __CFRUNLOOP_IS_CALLING_OUT_TO_A_BLOCK__ + 12
27 CoreFoundation 0x00007fff9e1789d4 __CFRunLoopDoBlocks + 356
28 CoreFoundation 0x00007fff9e178516 __CFRunLoopRun + 1894
29 CoreFoundation 0x00007fff9e177b54 CFRunLoopRunSpecific + 420
30 HIToolbox 0x00007fff9d702a5c RunCurrentEventLoopInMode + 240
31 HIToolbox 0x00007fff9d702891 ReceiveNextEventCommon + 432
32 HIToolbox 0x00007fff9d7026c6 _BlockUntilNextEventMatchingListInModeWithFilter + 71
33 AppKit 0x00007fff9bca85b4 _DPSNextEvent + 1120
34 AppKit 0x00007fff9c422d6b -[NSApplication(NSEvent) _nextEventMatchingEventMask:untilDate:inMode:dequeue:] + 2789
35 AppKit 0x00007fff9bc9cf35 -[NSApplication run] + 926
36 AppKit 0x00007fff9bc67850 NSApplicationMain + 1237
37 CookRecipes 0x000000010000c344 main + 84
38 libdyld.dylib 0x00007fffb3774255 start + 1
39 ??? 0x0000000000000003 0x0 + 3
)
2017-01-30 19:36:54.239473 CookRecipes[5004:203650] -[NSVBSavePanel observeValueForKeyPath:ofObject:change:context:] caught non-fatal NSInvalidArgumentException '-[NSScrollView reloadData]: unrecognized selector sent to instance 0x6180001c1770' with backtrace (
0 CoreFoundation 0x00007fff9e2010db __exceptionPreprocess + 171
1 libobjc.A.dylib 0x00007fffb2e92a2a objc_exception_throw + 48
2 CoreFoundation 0x00007fff9e281cd4 -[NSObject(NSObject) doesNotRecognizeSelector:] + 132
3 CoreFoundation 0x00007fff9e1742a3 ___forwarding___ + 1059
4 CoreFoundation 0x00007fff9e173df8 _CF_forwarding_prep_0 + 120
5 CookRecipes 0x0000000100004239 _TFFC11CookRecipes8Document7addFileFT_T_U_FSiT_ + 457
6 CookRecipes 0x00000001000047ef _TTRXFo_dSi__XFdCb_dSi__ + 47
7 AppKit 0x00007fff9c0665e4 -[NSVBSavePanel didEndPanelWithReturnCode:] + 68
8 AppKit 0x00007fff9c6ba09d -[NSVBSavePanel completeModeless:] + 74
9 AppKit 0x00007fff9befa721 -[NSVBSavePanel observeValueForKeyPath:ofObject:change:context:] + 597
10 Foundation 0x00007fff9fbcb71d NSKeyValueNotifyObserver + 350
11 Foundation 0x00007fff9fbcafb4 NSKeyValueDidChange + 486
12 Foundation 0x00007fff9fb99e96 -[NSObject(NSKeyValueObserverNotification) didChangeValueForKey:] + 118
13 ViewBridge 0x00007fffb184c066 __41-[NSViewBridge setObject:forKey:withKVO:]_block_invoke + 447
14 ViewBridge 0x00007fffb18d052b withHintInProgress + 487
15 ViewBridge 0x00007fffb184be83 -[NSViewBridge setObject:forKey:withKVO:] + 1014
16 ViewBridge 0x00007fffb184a95a -[NSViewBridge nonLocalChangeInProgress:block:] + 390
17 ViewBridge 0x00007fffb18aa260 -[NSRemoteViewMarshal exceptionSafeSetRemoteObject:forKey:withReply:] + 256
18 ViewBridge 0x00007fffb18aa3e8 -[NSRemoteViewMarshal setRemoteObject:forKey:withReply:] + 56
19 CoreFoundation 0x00007fff9e1756dc __invoking___ + 140
20 CoreFoundation 0x00007fff9e175561 -[NSInvocation invoke] + 289
21 ViewBridge 0x00007fffb18d052b withHintInProgress + 487
22 ViewBridge 0x00007fffb18d025a __deferNSXPCInvocationOntoMainThread_block_invoke_2 + 233
23 ViewBridge 0x00007fffb1851df2 +[NSViewServiceApplication withHostPID:invoke:] + 46
24 ViewBridge 0x00007fffb18d011d __deferNSXPCInvocationOntoMainThread_block_invoke + 179
25 ViewBridge 0x00007fffb18cfd0a __deferBlockOntoMainThread_block_invoke_2 + 544
26 CoreFoundation 0x00007fff9e19771c __CFRUNLOOP_IS_CALLING_OUT_TO_A_BLOCK__ + 12
27 CoreFoundation 0x00007fff9e1789d4 __CFRunLoopDoBlocks + 356
28 CoreFoundation 0x00007fff9e178516 __CFRunLoopRun + 1894
29 CoreFoundation 0x00007fff9e177b54 CFRunLoopRunSpecific + 420
30 HIToolbox 0x00007fff9d702a5c RunCurrentEventLoopInMode + 240
31 HIToolbox 0x00007fff9d702891 ReceiveNextEventCommon + 432
32 HIToolbox 0x00007fff9d7026c6 _BlockUntilNextEventMatchingListInModeWithFilter + 71
33 AppKit 0x00007fff9bca85b4 _DPSNextEvent + 1120
34 AppKit 0x00007fff9c422d6b -[NSApplication(NSEvent) _nextEventMatchingEventMask:untilDate:inMode:dequeue:] + 2789
35 AppKit 0x00007fff9bc9cf35 -[NSApplication run] + 926
36 AppKit 0x00007fff9bc67850 NSApplicationMain + 1237
37 CookRecipes 0x000000010000c344 main + 84
38 libdyld.dylib 0x00007fffb3774255 start + 1
39 ??? 0x0000000000000003 0x0 + 3
)
I think that the code that the problem is in should be in this segment:
extension Document : AddAttachmentDelegate {
internal func addFile() {
let panel = NSOpenPanel()
panel.allowsMultipleSelection = false
panel.canChooseDirectories = false
panel.canChooseFiles = true
panel.begin { (result) -> Void in
if result == NSModalResponseOK, let resultURL = panel.urls.first {
do {
try self.addAttachmentAtURL(url: resultURL)
self.attachmentsList?.reloadData()
} catch let error as NSError {
if let window = self.windowForSheet {
NSApp.presentError(error, modalFor: window, delegate: nil, didPresent: nil, contextInfo: nil)
} else {
NSApp.presentError(error)
}
}
}
}
}
}
I have no idea what is going on. Hope you can help.
EDIT: attachmentsList is connected by the following:
#IBOutlet weak var attachmentsList: NSCollectionView!

Invalid pairing of layout attributes when specifying constraints programmatically in code in swift VC

I am following the pattern used in the the example here :
https://github.com/lucasderraugh/AppleProg-Cocoa-Tutorials/blob/master/Lesson%2066/Lesson%2066/AppDelegate.swift
My VC (subclass of NSViewController) looks like this :
import Cocoa
class SecondViewController: NSViewController {
var leftView = ColorView()
var rightView = ColorView()
#IBOutlet weak var nameTextField: NSTextField!
override func viewDidLoad() {
super.viewDidLoad()
// Do view setup here.
}
override func viewDidAppear() {
leftView.translatesAutoresizingMaskIntoConstraints = false
rightView.translatesAutoresizingMaskIntoConstraints = false
let leftConstraints:[NSLayoutConstraint] = [
leftView.widthAnchor.constraintEqualToAnchor(self.view.topAnchor),
leftView.leftAnchor.constraintEqualToAnchor(self.view.leftAnchor),
leftView.widthAnchor.constraintEqualToConstant(100),
leftView.heightAnchor.constraintEqualToConstant(100)
]
self.view.addSubview(leftView)
NSLayoutConstraint.activateConstraints(leftConstraints)
}
override func prepareForSegue(segue: NSStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "SecondToFourthSegue" {
(segue.destinationController as! NSViewController).representedObject = nameTextField.stringValue
}
}
}
But I get the following error at runtime :
Invalid pairing of layout attributes
The full stack trace :
2016-04-26 00:54:06.402 Hyperterm[19046:1591173] An uncaught exception was raised
2016-04-26 00:54:06.403 Hyperterm[19046:1591173] *** +[NSLayoutConstraint constraintWithItem:attribute:relatedBy:toItem:attribute:multiplier:constant:]: Invalid pairing of layout attributes
2016-04-26 00:54:06.403 Hyperterm[19046:1591173] (
0 CoreFoundation 0x00007fff89cf94f2 __exceptionPreprocess + 178
1 libobjc.A.dylib 0x00007fff8b89b73c objc_exception_throw + 48
2 CoreFoundation 0x00007fff89d604bd +[NSException raise:format:] + 205
3 Foundation 0x00007fff8d717f92 VerifyConstraintArguments + 356
4 Foundation 0x00007fff8d717c16 +[NSLayoutConstraint constraintWithItem:attribute:relatedBy:toItem:attribute:multiplier:constant:] + 295
5 Foundation 0x00007fff8d71d410 -[NSLayoutAnchor constraintEqualToAnchor:multiplier:constant:] + 149
6 Foundation 0x00007fff8d71e87f -[NSLayoutDimension constraintEqualToAnchor:multiplier:constant:] + 39
7 Hyperterm 0x000000010001bee1 _TFC9Hyperterm20SecondViewController13viewDidAppearfT_T_ + 385
8 Hyperterm 0x000000010001c4b2 _TToFC9Hyperterm20SecondViewController13viewDidAppearfT_T_ + 34
9 libdispatch.dylib 0x0000000100607070 _dispatch_call_block_and_release + 12
10 libdispatch.dylib 0x00000001005f9cc5 _dispatch_client_callout + 8
11 libdispatch.dylib 0x00000001006116ae _dispatch_main_queue_callback_4CF + 2845
12 CoreFoundation 0x00007fff89cae9e9 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 9
13 CoreFoundation 0x00007fff89c6d8dd __CFRunLoopRun + 1949
14 CoreFoundation 0x00007fff89c6ced8 CFRunLoopRunSpecific + 296
15 HIToolbox 0x00007fff949a7935 RunCurrentEventLoopInMode + 235
16 HIToolbox 0x00007fff949a776f ReceiveNextEventCommon + 432
17 HIToolbox 0x00007fff949a75af _BlockUntilNextEventMatchingListInModeWithFilter + 71
18 AppKit 0x00007fff878aeefa _DPSNextEvent + 1067
19 AppKit 0x00007fff878ae32a -[NSApplication _nextEventMatchingEventMask:untilDate:inMode:dequeue:] + 454
20 AppKit 0x00007fff878a2e84 -[NSApplication run] + 682
21 AppKit 0x00007fff8786c46c NSApplicationMain + 1176
22 Hyperterm 0x000000010001b724 main + 84
23 libdyld.dylib 0x00007fff8fc285ad start + 1
24 ??? 0x0000000000000003 0x0 + 3
)
2016-04-26 00:54:06.405 Hyperterm[19046:1591173] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** +[NSLayoutConstraint constraintWithItem:attribute:relatedBy:toItem:attribute:multiplier:constant:]: Invalid pairing of layout attributes'
*** First throw call stack:
(
0 CoreFoundation 0x00007fff89cf94f2 __exceptionPreprocess + 178
1 libobjc.A.dylib 0x00007fff8b89b73c objc_exception_throw + 48
2 CoreFoundation 0x00007fff89d604bd +[NSException raise:format:] + 205
3 Foundation 0x00007fff8d717f92 VerifyConstraintArguments + 356
4 Foundation 0x00007fff8d717c16 +[NSLayoutConstraint constraintWithItem:attribute:relatedBy:toItem:attribute:multiplier:constant:] + 295
5 Foundation 0x00007fff8d71d410 -[NSLayoutAnchor constraintEqualToAnchor:multiplier:constant:] + 149
6 Foundation 0x00007fff8d71e87f -[NSLayoutDimension constraintEqualToAnchor:multiplier:constant:] + 39
7 Hyperterm 0x000000010001bee1 _TFC9Hyperterm20SecondViewController13viewDidAppearfT_T_ + 385
8 Hyperterm 0x000000010001c4b2 _TToFC9Hyperterm20SecondViewController13viewDidAppearfT_T_ + 34
9 libdispatch.dylib 0x0000000100607070 _dispatch_call_block_and_release + 12
10 libdispatch.dylib 0x00000001005f9cc5 _dispatch_client_callout + 8
11 libdispatch.dylib 0x00000001006116ae _dispatch_main_queue_callback_4CF + 2845
12 CoreFoundation 0x00007fff89cae9e9 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 9
13 CoreFoundation 0x00007fff89c6d8dd __CFRunLoopRun + 1949
14 CoreFoundation 0x00007fff89c6ced8 CFRunLoopRunSpecific + 296
15 HIToolbox 0x00007fff949a7935 RunCurrentEventLoopInMode + 235
16 HIToolbox 0x00007fff949a776f ReceiveNextEventCommon + 432
17 HIToolbox 0x00007fff949a75af _BlockUntilNextEventMatchingListInModeWithFilter + 71
18 AppKit 0x00007fff878aeefa _DPSNextEvent + 1067
19 AppKit 0x00007fff878ae32a -[NSApplication _nextEventMatchingEventMask:untilDate:inMode:dequeue:] + 454
20 AppKit 0x00007fff878a2e84 -[NSApplication run] + 682
21 AppKit 0x00007fff8786c46c NSApplicationMain + 1176
22 Hyperterm 0x000000010001b724 main + 84
23 libdyld.dylib 0x00007fff8fc285ad start + 1
24 ??? 0x0000000000000003 0x0 + 3
)
libc++abi.dylib: terminating with uncaught exception of type NSException
leftView.widthAnchor should not equal to self.view.topAnchor You might not pay attention to it