你如何将这个enumerateDraggingItemsWithOptions方法调用转换为Swift?

时间:2014-10-26 05:07:23

标签: objective-c macos cocoa swift

名为CocoaDragAndDrop的示例应用程序使用名为enumerateDraggingItemsWithOptions的方法。这是在Obj-C:

    [sender enumerateDraggingItemsWithOptions:NSDraggingItemEnumerationConcurrent 
        forView:self
        classes:[NSArray arrayWithObject:[NSPasteboardItem class]] 
        searchOptions:nil 
        usingBlock:^(NSDraggingItem *draggingItem, NSInteger idx, BOOL *stop) {


            if ( ![[[draggingItem item] types] containsObject:kPrivateDragUTI] ) {

                *stop = YES;

            } else {

                [draggingItem setDraggingFrame:self.bounds contents:[[[draggingItem imageComponents] objectAtIndex:0] contents]];
            }
        }];

你如何在swift中编写这段代码?

我试过这样:

sender.enumerateDraggingItemsWithOptions(
    enumOptions: NSDraggingItemEnumerationOptions.Concurrent,
    forView: self,
    classes: [NSPasteboardItem.self],
    searchOptions: NSPasteboardURLReadingFileURLsOnlyKey,
    usingBlock: { (draggingItem: NSDraggingItem, idx: Int, stop: UnsafeMutablePointer<ObjCBool>) -> Void in

        // ...

    }
)

我收到以下错误:

Cannot convert the expression's type '(enumOptions: NSDraggingItemEnumerationOptions, forView: DragDropImageView, classes: $T5, searchOptions: NSString!, usingBlock: (NSDraggingItem, Int, UnsafeMutablePointer<ObjCBool>) -> Void)' to type 'NSPasteboardItem.Type'

3 个答案:

答案 0 :(得分:2)

Swift 中的方法签名是

func enumerateDraggingItemsWithOptions(enumOpts: NSDraggingItemEnumerationOptions, 
  forView view: NSView, classes 
  classArray: [AnyObject], 
  searchOptions: [NSObject : AnyObject], 
  usingBlock block: (NSDraggingItem!, Int, UnsafeMutablePointer<ObjCBool>) -> Void)
  • 我们可以在回调中省略参数类型,因为它们来自声明, 使代码更干净
  • 回调是最后一个参数,因此我们可以使用trailing closure语法

对方法的调用

sender.enumerateDraggingItemsWithOptions(
  .Concurrent,
  forView: self,
  classes: [NSPasteboardItem.self],
  searchOptions: [NSPasteboardURLReadingFileURLsOnlyKey:self]) 
    {(draggingItem, idx, stop)  in
    // Code here
  }

答案 1 :(得分:1)

OS X 10.11中的签名已更改。

func enumerateDraggingItemsWithOptions(
  enumOpts: NSDraggingItemEnumerationOptions,
  forView view: NSView, 
  classes classArray: [AnyClass], 
  searchOptions: [String : AnyObject], 
  usingBlock block: (NSDraggingItem!, Int, UnsafeMutablePointer<ObjCBool>) -> Void) 

//所以,我们需要调整如下:

func f(sender:NSDraggingInfo) -> () {
  var v:NSView = NSView()
  let classArray:[AnyClass] = [NSPasteboardItem.self]
  let item = NSPasteboardItem()
  let searchOptions:[String:AnyObject] = [NSPasteboardURLReadingFileURLsOnlyKey:v]

答案 2 :(得分:0)

这个编译,因此显示正确的语法。我明确定义了参数以澄清类型:

import Foundation
import AppKit

func f(sender:NSDraggingInfo) -> () {

   var v:NSView = NSView()
   let classArray:[AnyObject] = [NSPasteboardItem.self]
   let item = NSPasteboardItem()
   let searchOptions:[NSObject:AnyObject] = [NSPasteboardURLReadingFileURLsOnlyKey:v]

sender.enumerateDraggingItemsWithOptions(
  NSDraggingItemEnumerationOptions.Concurrent,
  forView: v,
  classes: classArray,
  searchOptions: searchOptions,
  usingBlock: { (draggingItem: NSDraggingItem!, idx: Int, stop: UnsafeMutablePointer<ObjCBool>) -> Void in

    // ...

  }
)
}