Mojave - Drag and Drop from NSTableView fails if more items are written than rows dragged - drag-and-drop

I'm getting this error when dragging multiple items in OSX Mojave:
[General] There are 2 items on the pasteboard, but 1 drag images. There must be 1 draggingItem per pasteboardItem.
2018-09-05 14:09:57.176486+0200 TableViewPasteboardCrash[3106:66642] [General] (
0 CoreFoundation 0x00007fff4c1dd43d __exceptionPreprocess + 256
1 libobjc.A.dylib 0x00007fff7802b720 objc_exception_throw + 48
2 CoreFoundation 0x00007fff4c1dd26f +[NSException raise:format:] + 201
3 AppKit 0x00007fff49a03387 -[NSDraggingSession(NSInternal) _initWithPasteboard:image:offset:source:] + 247
4 AppKit 0x00007fff49a02d9f -[NSCoreDragManager dragImage:fromWindow:at:offset:event:pasteboard:source:slideBack:] + 1919
5 AppKit 0x00007fff49a02611 -[NSWindow(NSDrag) dragImage:at:offset:event:pasteboard:source:slideBack:] + 134
6 AppKit 0x00007fff49eb9a75 -[NSTableView _doImageDragUsingRowsWithIndexes:event:pasteboard:source:slideBack:startRow:] + 656
7 AppKit 0x00007fff49eb9f09 -[NSTableView __doImageDragUsingRowsWithIndexes:event:pasteboard:source:slideBack:startRow:] + 276
8 AppKit 0x00007fff49ebac9e -[NSTableView _performClassicDragOfIndexes:hitRow:event:] + 466
9 AppKit 0x00007fff49a31cf8 -[NSTableView _performDragFromMouseDown:] + 474
10 AppKit 0x00007fff49a2fffc -[NSTableView mouseDown:] + 798
11 AppKit 0x00007fff498b46ef -[NSWindow(NSEventRouting) _handleMouseDownEvent:isDelayedEvent:] + 5668
12 AppKit 0x00007fff497e872f -[NSWindow(NSEventRouting) _reallySendEvent:isDelayedEvent:] + 2319
13 AppKit 0x00007fff497e7bd5 -[NSWindow(NSEventRouting) sendEvent:] + 481
)
Does anyone have the same problem or a solution to drag multiple items ?

It looks like a bug in macOS 10.14.
There's actually a rdar for that: https://openradar.appspot.com/44135683
Meanwhile, there's a simple workaround.
I assume you are using NSPasteboard.writeItems, with a custom data class implementing NSPasteboardWriting.
I can definitely confirm this crashes on macOS 10.14.
Instead of NSPasteboardWriting, you should adopt NSCoding.
This means implementing:
init?( coder: NSCoder )
func encode( with coder: NSCoder )
Then, instead of using NSPasteboard.writeItems, use NSPasteboard.setPropertyList:
let items = [ MyItem ]() /* MyItem conforms to NSCoding */
/* Retrieve items */
pboard.setPropertyList( items, forType: "someTypeString" )
This won't crash with multiple items.
You'll then be able to retrieve your items with NSPasteboard.propertyList:
let items = pboard.propertyList( forType: "someTypeString" ) as? [ MyItem ]

Related

How to force a macOS app to update while being in the background?

I'm coding a macOS app with Xcode (XCode 11.5 under MacOS Catalina), the goal being to send through UDP the coordinates of fingers in contact with my Macbook Pro's trackpad, and trigger an haptic feedback when a UDP message is received.
Being a novice in Xcode, I created my app with this code, and SwiftOSC for the UDP communication. I succeeded to do what I wanted when the app is in the foreground.
Code: exactly the one found here.
Problem: the app is not updated while being in the background.
Update: Following #zrzka advices:
I use
NSEvent.addGlobalMonitorForEvents(matching: NSEvent.EventTypeMask.any) {event in
self.backMouseMovements(with: event)
}
to monitor events in the background, with the following function:
func backMouseMovements(with event: NSEvent) {
print("Event: \(event)")
let touches = event.touches(matching: .moved, in: self)
delegate?.touchesView(self, didUpdateTouchingTouches: touches)
}
I put this code in override init(frame frameRect: NSRect) of the AppKitTouchesView class of this code.
Accessibility: AXIsProcessTrusted() returns true
I don't have access to touches with these events, and even less to normalizedPosition, which is my ultimate goal.
Instead, I have an error:
2020-08-13 09:40:53.538119+0200 TrackPad[1263:34805] *** Assertion failure in -[NSEvent touchesMatchingPhase:inView:], /AppleInternal/BuildRoot/Library/Caches/com.apple.xbs/Sources/AppKit/AppKit-1894.50.103/AppKit.subproj/NSEvent.m:4874
2020-08-13 09:40:53.538198+0200 TrackPad[1263:34805] [General] An uncaught exception was raised
2020-08-13 09:40:53.538229+0200 TrackPad[1263:34805] [General] Invalid message sent to event "NSEvent: type=MouseMoved loc=(1197.92,994.461) time=3138.3 flags=0 win=0x0 winNum=150 ctxt=0x0 evNum=307 click=1 buttonNumber=0 pressure=0 deltaX=1.000000 deltaY=0.000000 deviceID:0x300000080500000 subtype=NSEventSubtypeTouch"
2020-08-13 09:40:53.539294+0200 TrackPad[1263:34805] [General] (
0 CoreFoundation 0x00007fff318c5be7 __exceptionPreprocess + 250
1 libobjc.A.dylib 0x00007fff6a69d5bf objc_exception_throw + 48
2 CoreFoundation 0x00007fff318eed98 +[NSException raise:format:arguments:] + 88
3 Foundation 0x00007fff33fdde9d -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 191
4 AppKit 0x00007fff2ee7fc36 -[NSEvent touchesMatchingPhase:inView:] + 449
5 TrackPad 0x0000000100002d0d $s8TrackPad17AppKitTouchesViewC18backMouseMovements4withySo7NSEventC_tF + 813
6 TrackPad 0x0000000100002986 $s8TrackPad17AppKitTouchesViewC5frameACSo6CGRectV_tcfcySo7NSEventCcfU_ + 214
7 TrackPad 0x0000000100002f09 $sSo7NSEventCIegg_ABIeyBy_TR + 73
8 AppKit 0x00007fff2f0a14e9 GlobalObserverHandler + 89
9 HIToolbox 0x00007fff3044f8ff _ZL23DispatchEventToHandlersP14EventTargetRecP14OpaqueEventRefP14HandlerCallRec + 1254
10 HIToolbox 0x00007fff3044ed8d _ZL30SendEventToEventTargetInternalP14OpaqueEventRefP20OpaqueEventTargetRefP14HandlerCallRec + 329
11 HIToolbox 0x00007fff3044ec3d SendEventToEventTargetWithOptions + 45
12 HIToolbox 0x00007fff3048eb8c _ZL29ToolboxEventDispatcherHandlerP25OpaqueEventHandlerCallRefP14OpaqueEventRefPv + 1329
13 HIToolbox 0x00007fff3044fe8a _ZL23DispatchEventToHandlersP14EventTargetRecP14OpaqueEventRefP14HandlerCallRec + 2673
14 HIToolbox 0x00007fff3044ed8d _ZL30SendEventToEventTargetInternalP14OpaqueEventRefP20OpaqueEventTargetRefP14HandlerCallRec + 329
15 HIToolbox 0x00007fff3046447e SendEventToEventTarget + 39
16 AppKit 0x00007fff2eabe9aa _DPSNextEvent + 1268
17 AppKit 0x00007fff2eabd070 -[NSApplication(NSEvent) _nextEventMatchingEventMask:untilDate:inMode:dequeue:] + 1352
18 AppKit 0x00007fff2eaaed7e -[NSApplication run] + 658
19 AppKit 0x00007fff2ea80b86 NSApplicationMain + 777
20 TrackPad 0x000000010000eead main + 13
21 libdyld.dylib 0x00007fff6b844cc9 start + 1
I guess this error is thrown because I want to access to touches, which doesn't exist for these events. However, I don't understand how to make a link between the NSEvent of my addGlobalMonitorForEvents and NSTouch, where normalizedPosition is available.
Thanks!
I finally found a solution with this framework: https://github.com/Kyome22/OpenMultitouchSupport
Thanks for your help!

Error on handling mouse events on cocoa

I've got a custom ViewController, a custom WindowController and a custom View. The ViewController is connected to the WindowController and the View is connected to the ViewController.
In my ViewController I have the following code for the mouse events:
override func mouseDown(with event: NSEvent) {
//code
}
override func mouseUp(with event: NSEvent) {
//code
}
On launching this, I'm getting the following error message and furthermore on debugging these methods are not even entered:
2018-03-08 11:27:11.594158+0100 MyProject[2697:83761] *** Assertion failure in -[NSEvent touchesMatchingPhase:inView:], /Library/Caches/com.apple.xbs/Sources/AppKit/AppKit-1504.83.101/AppKit.subproj/NSEvent.m:4654
2018-03-08 11:27:11.594574+0100 MyProject[2697:83761] [General] Invalid message sent to event "NSEvent: type=LMouseDown loc=(701.219,422.156) time=8788.4 flags=0 win=0x6080001e0200 winNum=1255 ctxt=0x0 evNum=1071 click=1 buttonNumber=0 pressure=1 deviceID:0x300000014400000 subtype=NSEventSubtypeTouch"
2018-03-08 11:27:11.599387+0100 MyProject[2697:83761] [General] (
0 CoreFoundation 0x00007fff8b59d2cb __exceptionPreprocess + 171
1 libobjc.A.dylib 0x00007fffa03b548d objc_exception_throw + 48
2 CoreFoundation 0x00007fff8b5a2042 +[NSException raise:format:arguments:] + 98
3 Foundation 0x00007fff8cfeabe0 -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 195
4 AppKit 0x00007fff89327573 -[NSEvent touchesMatchingPhase:inView:] + 205
5 MyProject 0x0000000100013c0b _T015MyProject18MyViewControllerC9mouseDownySo7NSEventC4with_tF + 139
6 MyProject 0x0000000100014bbc _T015MyProject18MyViewControllerC9mouseDownySo7NSEventC4with_tFTo + 60
7 AppKit 0x00007fff891897a9 forwardMethod + 133
8 AppKit 0x00007fff891897a9 forwardMethod + 133
9 AppKit 0x00007fff8990424f -[NSWindow(NSEventRouting) _handleMouseDownEvent:isDelayedEvent:] + 6341
10 AppKit 0x00007fff89900a6c -[NSWindow(NSEventRouting) _reallySendEvent:isDelayedEvent:] + 1942
11 AppKit 0x00007fff898fff0a -[NSWindow(NSEventRouting) sendEvent:] + 541
12 AppKit 0x00007fff89784681 -[NSApplication(NSEvent) sendEvent:] + 1145
13 AppKit 0x00007fff88fff427 -[NSApplication run] + 1002
14 AppKit 0x00007fff88fc9e0e NSApplicationMain + 1237
15 MyProject 0x0000000100023add main + 13
16 libdyld.dylib 0x00007fffa0c9b235 start + 1
)
I've already tried the following things:
Register these both methods in the WindowController and just write in each:
let vc = self.nextResponder as! MyViewController
vc.mouseUp // or vc.mouseDown
--> still getting the above error
Register these both methods in the viewDidLoad() / windowDidLoad() by writing the following Code:
NSEvent.addLocalMonitorForEvents(matching: .leftMouseDown){
self.mouseDown(with: $0)
return $0
}
NSEvent.addLocalMonitorForEvents(matching: .leftMouseUp){
self.mouseUp(with: $0)
return $0
}
--> still getting the above error
Question: How can I make these both methods work in my ViewController?
Important Note: Please consider that I am using Swift 4.0.2 and Xcode 9.1 Build 9B55
The error:
Assertion failure in -[NSEvent touchesMatchingPhase:inView:]
Invalid message sent to event "NSEvent: type=LMouseDown
touches(matching:in:) is called from mouseDown(with:). The documentation of touches(matching:in:) says:
This method is only valid for gesture events (gesture, magnify, swipe, rotate, etc.).

[NSView reloadData]: unrecognized selector sent to instance

I am trying to learn a bit of Swift and xcode by creating a three-level outlineView in the master side of a splitView. The code crashes after exiting to reloadData (see below). It makes one successful pass through the NSOutlineViewDataSource numberOfChildrenOfItem function (I expected a second pass before anything else happened) and then crashes with the "[NSView reloadData]:unrecognized selector sent to instance" error. Here is what is probably the relevant code:
import Cocoa
// import WebKit
class MasterViewController: NSViewController {
#IBOutlet weak var outlineView: NSOutlineView!
var categories = [Category]()
override func viewDidLoad() {
super.viewDidLoad()
// Do view setup here.
if let filePath = Bundle.main.path(forResource: "Subjects", ofType: "plist"){
categories = Category.categoryList(filePath)
print(categories)
}
self.outlineView.reloadData()
}
}
//These are extension functions required by a NSOutlineViewDataSource
/*
Returns the number of children or grandchildren.
One thing to note: item is an optional, and will be nil
for the root objects of your data model. In this case,
it will be nil for Category; otherwise it will contain
the parent of the object. For categoryItem objects,
item will be a Category.
*/
extension MasterViewController: NSOutlineViewDataSource {
func outlineView(_ outlineView: NSOutlineView,
numberOfChildrenOfItem item: Any?) -> Int {
if let category = item as? Category {
return category.children.count
}else if let categoryItem = item as? CategoryItem {
return categoryItem.grandchildren.count
}
return categories.count
}
And here is the error dump (I am not knowledgeable enough to make much of this):
[<ProbleMatic.Category: 0x600000048190>, <ProbleMatic.Category: 0x604000049630>, <ProbleMatic.Category: 0x60400004e100>]
2017-12-11 11:16:30.361189-0700 ProbleMatic[11382:4324228] -[NSView reloadData]: unrecognized selector sent to instance 0x6000001237a0
2017-12-11 11:16:30.361701-0700 ProbleMatic[11382:4324228] [General] -[NSView reloadData]: unrecognized selector sent to instance 0x6000001237a0
2017-12-11 11:16:30.365690-0700 ProbleMatic[11382:4324228] [General] (
0 CoreFoundation 0x00007fff390b300b
__exceptionPreprocess + 171
1 libobjc.A.dylib 0x00007fff5fc91c76
objc_exception_throw + 48
2 CoreFoundation 0x00007fff3914bcd4
-[NSObject(NSObject) doesNotRecognizeSelector:] + 132
3 CoreFoundation 0x00007fff390293f0
___forwarding___ + 1456
4 CoreFoundation 0x00007fff39028db8
_CF_forwarding_prep_0 + 120
5 ProbleMatic 0x0000000100001cbc
_T011ProbleMatic20MasterViewControllerC11viewDidLoadyyF + 1148
6 ProbleMatic 0x0000000100001e44
_T011ProbleMatic20MasterViewControllerC11viewDidLoadyyFTo + 36
7 AppKit 0x00007fff3665b632
-[NSViewController _sendViewDidLoad] + 97
8 AppKit 0x00007fff36bffd99
_noteLoadCompletionForObject + 641
9 AppKit 0x00007fff365dfe21
-[NSIBObjectData nibInstantiateWithOwner:options:topLevelObjects:] + 2042
10 AppKit 0x00007fff366db13a
-[NSNib _instantiateNibWithExternalNameTable:options:] + 679
11 AppKit 0x00007fff366dad96
-[NSNib _instantiateWithOwner:options:topLevelObjects:] + 136
12 AppKit 0x00007fff366da06c
-[NSViewController loadView] + 343
13 AppKit 0x00007fff36651f32
-[NSViewController _loadViewIfRequired] + 75
14 AppKit 0x00007fff36651e9d
-[NSViewController view] + 30
15 AppKit 0x00007fff3666c96b
-[_NSSplitViewItemViewWrapper wrapView] + 94
16 AppKit 0x00007fff3666319e
-[NSSplitViewController _setupSplitView] + 593
17 AppKit 0x00007fff36662d15
-[NSSplitViewController viewDidLoad] + 155
18 AppKit 0x00007fff3665b632
-[NSViewController _sendViewDidLoad] + 97
19 AppKit 0x00007fff3665206d
-[NSViewController _loadViewIfRequired] + 390
20 AppKit 0x00007fff36651e9d
-[NSViewController view] + 30
21 AppKit 0x00007fff36aa4cc7
__67-[NSStoryboardEmbedSegueTemplate newDefaultPerformHandlerForSegue:]_block_invoke + 171
22 AppKit 0x00007fff36652015
-[NSViewController _loadViewIfRequired] + 302
23 AppKit 0x00007fff36651e9d
-[NSViewController view] + 30
24 AppKit 0x00007fff36eeaa23
+[NSWindow windowWithContentViewController:] + 41
25 AppKit 0x00007fff3714c44d
+[NSWindowController windowControllerWithContentViewController:] + 41
26 AppKit 0x00007fff36aa4888
__66-[NSStoryboardShowSegueTemplate newDefaultPerformHandlerForSegue:]_block_invoke + 172
27 AppKit 0x00007fff36aa4602
-[NSStoryboardShowSegueTemplate _perform:] + 275
28 AppKit 0x00007fff36da775a
-[NSApplication(NSResponder) sendAction:to:from:] + 312
29 AppKit 0x00007fff3684d933
-[NSControl sendAction:to:] + 86
30 AppKit 0x00007fff3684d85b
__26-[NSCell _sendActionFrom:]_block_invoke + 136
31 AppKit 0x00007fff3684d761
-[NSCell _sendActionFrom:] + 183
32 AppKit 0x00007fff3688ea18
-[NSButtonCell _sendActionFrom:] + 97
33 AppKit 0x00007fff3684bfca
-[NSCell trackMouse:inRect:ofView:untilMouseUp:] + 2438
34 AppKit 0x00007fff3688e75f
-[NSButtonCell trackMouse:inRect:ofView:untilMouseUp:] + 777
35 AppKit 0x00007fff3684aa64
-[NSControl mouseDown:] + 965
36 AppKit 0x00007fff36f46959
-[NSWindow(NSEventRouting) _handleMouseDownEvent:isDelayedEvent:] + 5891
37 AppKit 0x00007fff36f435b0
-[NSWindow(NSEventRouting) _reallySendEvent:isDelayedEvent:] + 2359
38 AppKit 0x00007fff36f4285c
-[NSWindow(NSEventRouting) sendEvent:] + 497
39 AppKit 0x00007fff36da3617
-[NSApplication(NSEvent) sendEvent:] + 307
40 AppKit 0x00007fff36604d9d
-[NSApplication run] + 812
41 AppKit 0x00007fff365d3f1a
NSApplicationMain + 804
42 ProbleMatic 0x000000010000f50d
main + 13
43 libdyld.dylib 0x00007fff60881115
start + 1
44 ??? 0x0000000000000003
0x0 + 3
)
Any suggestions? Thanks in advance.
Your outlet is probably connected to something that isn't the outline view (Most likely its superview).
You can check by…
Hovering your mouse over the outlet circle
or ctrl clicking the outline view in the view hierarchy.

Drag and Drop in Swift - Issues with Registering for Dragged Types?

Background
I am trying to do some simple Drag and Drop in Swift similar to the sample code from Apple for Cocoa Drag and Drop. I am not getting far before I have errors.
I have created a swift class dropView with code at bottom. self.registered for types seems like it worked as I get a long list of image types or the shorter list.
When I set the array for registering dragging types to TIF and jpeg types, I get no response for draggingEntered or draggingUpdated. It seems that I am missing something simple? I have the custom view set to the dropView class.
Separately, I get a host of errors when I drag a file (at least for TIF and jpeg) when the array is set to NSImage.imagePasteBoardTypes() see ** at bottom of post.
Questions
Why is draggingEntered or draggingUpdated not being called?
Other questions:
Am I registering for dragged types correctly?
Is the array in the right format?
Is the NSDraggingDestination property bit at the top of the file in the right spot?
Code
import Cocoa
class dropView: NSView, NSDraggingDestination {
init(frame: NSRect) {
super.init(frame: frame)
//let theArray = [NSImage.imagePasteboardTypes()]
let theArray = ["NSTypedFilenamesPboardType:jpg",
"NSTypedFilenamesPboardType:JPG",
"NSTypedFilenamesPboardType:jpeg",
"NSTypedFilenamesPboardType:JPEG",
"NSTypedFilenamesPboardType:jpe",
"NSTypedFilenamesPboardType:TIF"]
registerForDraggedTypes(theArray)
println("INIT and REGISTER")
println(self.registeredDraggedTypes)
}
override func drawRect(dirtyRect: NSRect) {
super.drawRect(dirtyRect)
// Drawing code here.
}
override func draggingEntered(sender: NSDraggingInfo!) -> NSDragOperation {
println("Dragging Entered")
return NSDragOperation.Copy
}
override func draggingUpdated(sender: NSDraggingInfo!) -> NSDragOperation {
println("UPDATED")
return NSDragOperation.Copy
}
}
** Errors when NSImage.imagePasteBoardTypes()
2014-06-21 11:34:38.728 DragAndDrop[96606:303] -[__NSArrayM length]: unrecognized selector sent to instance 0x610000049d80
2014-06-21 11:34:38.729 DragAndDrop[96606:303] -[__NSArrayM length]: unrecognized selector sent to instance 0x610000049d80
2014-06-21 11:34:38.730 DragAndDrop[96606:303] (
0 CoreFoundation 0x00007fff8ddaf25c __exceptionPreprocess + 172
1 libobjc.A.dylib 0x00007fff83a5de75 objc_exception_throw + 43
2 CoreFoundation 0x00007fff8ddb212d -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
3 CoreFoundation 0x00007fff8dd0d322 ___forwarding___ + 1010
4 CoreFoundation 0x00007fff8dd0cea8 _CF_forwarding_prep_0 + 120
5 LaunchServices 0x00007fff8aee1b0a XCFStringHashCaseInsensitive + 111
6 CoreFoundation 0x00007fff8dc6afd8 CFBasicHashFindBucket + 1032
7 CoreFoundation 0x00007fff8dc989c9 CFSetGetValueIfPresent + 121
8 CoreFoundation 0x00007fff8dc9892c -[__NSCFSet member:] + 28
9 CoreFoundation 0x00007fff8dccb008 -[NSSet containsObject:] + 24
10 CoreFoundation 0x00007fff8dd18fcf -[NSSet intersectsSet:] + 735
11 AppKit 0x00007fff86c645ff -[NSView(NSDrag) _hitTest:dragTypes:] + 221
12 AppKit 0x00007fff86c645d2 -[NSView(NSDrag) _hitTest:dragTypes:] + 176
13 AppKit 0x00007fff86c645d2 -[NSView(NSDrag) _hitTest:dragTypes:] + 176
14 AppKit 0x00007fff86c6438c -[NSWindow(NSDrag) _findDragTargetFrom:] + 111
15 AppKit 0x00007fff86c63280 NSCoreDragTrackingProc + 476
16 HIServices 0x00007fff8b8a05a3 DoEnterLeaveHandler + 389
17 HIServices 0x00007fff8b8a2fdd CoreDragMessageHandler + 1741
18 CoreFoundation 0x00007fff8dd5ace8 __CFMessagePortPerform + 760
19 CoreFoundation 0x00007fff8dce08d9 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 41
20 CoreFoundation 0x00007fff8dce084e __CFRunLoopDoSource1 + 478
21 CoreFoundation 0x00007fff8dcd1886 __CFRunLoopRun + 1830
22 CoreFoundation 0x00007fff8dcd0f25 CFRunLoopRunSpecific + 309
23 HIToolbox 0x00007fff8b908a0d RunCurrentEventLoopInMode + 226
24 HIToolbox 0x00007fff8b9087b7 ReceiveNextEventCommon + 479
25 HIToolbox 0x00007fff8b9085bc _BlockUntilNextEventMatchingListInModeWithFilter + 65
26 AppKit 0x00007fff8695726e _DPSNextEvent + 1434
27 AppKit 0x00007fff869568bb -[NSApplication nextEventMatchingMask:untilDate:inMode:dequeue:] + 122
28 AppKit 0x00007fff8694a9bc -[NSApplication run] + 553
29 AppKit 0x00007fff869357a3 NSApplicationMain + 940
30 DragAndDrop 0x00000001000033fd top_level_code + 109
31 DragAndDrop 0x000000010000343a main + 42
32 libdyld.dylib 0x00007fff8e1925fd start + 1
33 ??? 0x0000000000000003 0x0 + 3
)
2014-06-21 11:34:40.729 DragAndDrop[96606:303] -[__NSArrayM length]: unrecognized selector sent to instance 0x610000049d80
2014-06-21 11:34:40.730 DragAndDrop[96606:303] -[__NSArrayM length]: unrecognized selector sent to instance 0x610000049d80
2014-06-21 11:34:40.731 DragAndDrop[96606:303] (
0 CoreFoundation 0x00007fff8ddaf25c __exceptionPreprocess + 172
1 libobjc.A.dylib 0x00007fff83a5de75 objc_exception_throw + 43
2 CoreFoundation 0x00007fff8ddb212d -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
3 CoreFoundation 0x00007fff8dd0d322 ___forwarding___ + 1010
4 CoreFoundation 0x00007fff8dd0cea8 _CF_forwarding_prep_0 + 120
5 LaunchServices 0x00007fff8aee1b0a XCFStringHashCaseInsensitive + 111
6 CoreFoundation 0x00007fff8dc6afd8 CFBasicHashFindBucket + 1032
7 CoreFoundation 0x00007fff8dc989c9 CFSetGetValueIfPresent + 121
8 CoreFoundation 0x00007fff8dc9892c -[__NSCFSet member:] + 28
9 CoreFoundation 0x00007fff8dccb008 -[NSSet containsObject:] + 24
10 CoreFoundation 0x00007fff8dd18fcf -[NSSet intersectsSet:] + 735
11 AppKit 0x00007fff86c645ff -[NSView(NSDrag) _hitTest:dragTypes:] + 221
12 AppKit 0x00007fff86c645d2 -[NSView(NSDrag) _hitTest:dragTypes:] + 176
13 AppKit 0x00007fff86c645d2 -[NSView(NSDrag) _hitTest:dragTypes:] + 176
14 AppKit 0x00007fff86c6438c -[NSWindow(NSDrag) _findDragTargetFrom:] + 111
15 AppKit 0x00007fff86c63280 NSCoreDragTrackingProc + 476
16 HIServices 0x00007fff8b89fca4 DoTrackingMessage + 370
17 HIServices 0x00007fff8b8a2b36 CoreDragMessageHandler + 550
18 CoreFoundation 0x00007fff8dd5ace8 __CFMessagePortPerform + 760
19 CoreFoundation 0x00007fff8dce08d9 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 41
20 CoreFoundation 0x00007fff8dce084e __CFRunLoopDoSource1 + 478
21 CoreFoundation 0x00007fff8dcd1886 __CFRunLoopRun + 1830
22 CoreFoundation 0x00007fff8dcd0f25 CFRunLoopRunSpecific + 309
23 HIToolbox 0x00007fff8b908a0d RunCurrentEventLoopInMode + 226
24 HIToolbox 0x00007fff8b9087b7 ReceiveNextEventCommon + 479
25 HIToolbox 0x00007fff8b9085bc _BlockUntilNextEventMatchingListInModeWithFilter + 65
26 AppKit 0x00007fff8695726e _DPSNextEvent + 1434
27 AppKit 0x00007fff869568bb -[NSApplication nextEventMatchingMask:untilDate:inMode:dequeue:] + 122
28 AppKit 0x00007fff8694a9bc -[NSApplication run] + 553
29 AppKit 0x00007fff869357a3 NSApplicationMain + 940
30 DragAndDrop 0x00000001000033fd top_level_code + 109
31 DragAndDrop 0x000000010000343a main + 42
32 libdyld.dylib 0x00007fff8e1925fd start + 1
33 ??? 0x0000000000000003 0x0 + 3
)
(Swift 3, Xcode 8b6, OSX 10.11)
Apple recommends that the older style of pasteboard types should no longer be used and to use UTIs where possible - the better version is
self.register(forDraggedTypes: [kUTTypeURL as String])
This means that you receive drags from the Finder, for all filetypes.
Since NSImage.imagePasteboardTypes has been deprecated without replacement, you should handle the filtering in draggingEntered:
override func draggingEntered(_ sender: NSDraggingInfo) -> NSDragOperation {
let pasteboard = sender.draggingPasteboard()
let filteringOptions = [NSPasteboardURLReadingContentsConformToTypesKey:NSImage.imageTypes()]
if pasteboard.canReadObject(forClasses: [NSURL.self], options: filteringOptions) {
return NSDragOperation.copy
}
return NSDragOperation() //alternatively: []
}
(credit for the use of NSPasteboardURLReadingContentsConformToTypesKey goes to #TroutDev's tutorial here)
Regarding the crash
Separately, I get a host of errors when I drag a file (at least for TIF and jpeg) when the array is set to NSImage.imagePasteBoardTypes() see ** at bottom of post.
NSImage.imagePasteboardTypes() already returns an array, so using it like this:
let theArray = NSImage.imagePasteboardTypes()
instead of like this
let theArray = [NSImage.imagePasteboardTypes()]
will fix the crash.
Regarding NSDragDestination
Why is draggingEntered or draggingUpdated not being called?
If you're dragging from Finder, you probably want let types = [NSFilenamesPboardType]. The code you have means you have to place data onto the pasteboard yourself -- Finder doesn't put file contents onto the pasteboard, it puts filenames.
Is the NSDraggingDestination property bit at the top of the file in the right spot?
Yep! : NSDraggingDestination tells Swift that you're about to implement the protocol.
Working example
// Public domain
import Cocoa
class DragView: NSView, NSDraggingDestination {
init(frame: NSRect) {
super.init(frame: frame)
let types = [NSFilenamesPboardType]
registerForDraggedTypes(types)
println(self.registeredDraggedTypes)
}
override func drawRect(dirtyRect: NSRect) {
super.drawRect(dirtyRect)
NSColor.whiteColor().set()
NSRectFill(dirtyRect)
}
override func draggingEntered(sender: NSDraggingInfo!) -> NSDragOperation {
println("hello")
return NSDragOperation.Copy
}
}

Unlocking Focus on wrong view ((null)) under OS X 10.7.2

I have a Cocoa app that has an NSWindowController controlling an NSWindow with an NSComboBox in it. Nothing special, and it worked fine until upgrading to OS X 10.7.2. Now I get the following exception, which occurs only once, when the window controller's window loads. (Since the window controller in my app remains loaded, it happens only once per app launch.) Subsequently focusing and unfocusing the combo box does not trigger it.
Anyone know whether something changed in 10.7.2, and if so how to work around it?
(This is happening with Xcode 4.2.)
2011-10-24 11:30:21.649 MyApp[7934:707] Unlocking Focus on wrong view ((null)), expected <NSComboBox: 0x40083d6e0>
2011-10-24 11:30:21.653 MyApp[7934:707] (
0 CoreFoundation 0x00007fff8412d286 __exceptionPreprocess + 198
1 libobjc.A.dylib 0x00007fff88cdad5e objc_exception_throw + 43
2 CoreFoundation 0x00007fff8412d0ba +[NSException raise:format:arguments:] + 106
3 CoreFoundation 0x00007fff8412d044 +[NSException raise:format:] + 116
4 AppKit 0x00007fff8c76c1d5 -[NSFocusStack unfocusView:] + 194
5 AppKit 0x00007fff8c7e838e +[_NSAutomaticFocusRing showForView:] + 2545
6 AppKit 0x00007fff8cdbb4d1 __postActiveFirstResponderChanged_block_invoke_1 + 32
7 CoreFoundation 0x00007fff840ecf37 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23
8 CoreFoundation 0x00007fff840ece96 __CFRunLoopDoObservers + 374
9 CoreFoundation 0x00007fff840c2159 __CFRunLoopRun + 825
10 CoreFoundation 0x00007fff840c1ae6 CFRunLoopRunSpecific + 230
11 HIToolbox 0x00007fff8c0d73d3 RunCurrentEventLoopInMode + 277
12 HIToolbox 0x00007fff8c0de58f ReceiveNextEventCommon + 181
13 HIToolbox 0x00007fff8c0de4ca BlockUntilNextEventMatchingListInMode + 62
14 AppKit 0x00007fff8c71e3f1 _DPSNextEvent + 659
15 AppKit 0x00007fff8c71dcf5 -[NSApplication nextEventMatchingMask:untilDate:inMode:dequeue:] + 135
16 AppKit 0x00007fff8c71a62d -[NSApplication run] + 470
17 AppKit 0x00007fff8c99980c NSApplicationMain + 867
18 MyApp 0x0000000100001682 main + 34
19 MyApp 0x0000000100001654 start + 52
)
http://hints.macworld.com/index.php?topic=system107&page=5
This appears to have been a bug in OS X 10.7.* (Lion) series. With zero changes to my code, the crash no-longer occurs on OS X 10.8 (Mountain Lion) complied with Xcode 4.4.1.
You could try calling this command on the NSComboBox: setFocusRingType:NSFocusRingTypeNone
That should cure the problem, although it does not explain the difference between the two releases.