gpt4 book ai didi

objective-c - 您如何将此 enumerateDraggingItemsWithOptions 方法调用转换为 Swift?

转载 作者:可可西里 更新时间:2023-11-01 01:06:39 29 4
gpt4 key购买 nike

名为 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'

最佳答案

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
}

关于objective-c - 您如何将此 enumerateDraggingItemsWithOptions 方法调用转换为 Swift?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26569991/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com