swift 构建OSX拖放操场:将dragndrop.swift抛出到共享源中,然后测试单个playgro中的示例

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了swift 构建OSX拖放操场:将dragndrop.swift抛出到共享源中,然后测试单个playgro中的示例相关的知识,希望对你有一定的参考价值。

import Cocoa

// Support Foundation calls on String
public extension String { public var ns: NSString {return self as NSString} }

/// Custom Labeled Playground-Based Drag-and-Drop window
public class DropView: NSTextField {
    
    // Default action handler
    public var handler: ([String]) -> Void = { paths in Swift.print(paths) }

    // Drag and drop notification
    public override func draggingEntered(sender: NSDraggingInfo) -> NSDragOperation { return NSDragOperation.Copy }
    public override func draggingUpdated(sender: NSDraggingInfo) -> NSDragOperation { return NSDragOperation.Copy }
    public override func performDragOperation(sender: NSDraggingInfo) -> Bool {
        let pboard = sender.draggingPasteboard()
        guard let paths = pboard.propertyListForType(NSFilenamesPboardType) as? [String] else { return false }
        handler(paths)
        return true
    }
    
    public required init?(coder: NSCoder) { super.init(coder: coder) }
    public override init(frame frameRect: NSRect) {
        super.init(frame: frameRect)
        
        // Presentation
        stringValue = "Drop Here"; editable = false
        font = NSFont(name: "Palatino", size: 48.0); alignment = .Center
        wantsLayer = true; layer?.backgroundColor = NSColor.whiteColor().CGColor

        // Register for file name drags
        registerForDraggedTypes([NSFilenamesPboardType])
    }
}
import Cocoa
import XCPlayground

// Establish drop view
let dropView = DropView(frame: NSRect(x: 0, y: 0, width: 600, height: 200))
dropView.stringValue = "Drop text files here for word counts"
dropView.handler = {
    print("Word Counts")
    for path in $0 {
        guard let string = try? String(contentsOfFile: path) else { continue }
        print(path.ns.lastPathComponent, ":",
              string.ns.componentsSeparatedByString(" ").count)
    }
}

// Run live
XCPlaygroundPage.currentPage.liveView = dropView
XCPlaygroundPage.currentPage.needsIndefiniteExecution = true
import Cocoa
import XCPlayground

// Establish drop view
let dropView = DropView(frame: NSRect(x: 0, y: 0, width: 600, height: 200))
dropView.stringValue = "Drop Pictures"
dropView.handler = {
    // Print out each file name and image size
    $0.forEach {
        if let image = NSImage(contentsOfFile: $0) {
            Swift.print($0.ns.lastPathComponent, image.size)
        } else {
            Swift.print($0.ns.lastPathComponent, "is not an image")
        }
    }
}

// Run live
XCPlaygroundPage.currentPage.liveView = dropView
XCPlaygroundPage.currentPage.needsIndefiniteExecution = true

以上是关于swift 构建OSX拖放操场:将dragndrop.swift抛出到共享源中,然后测试单个playgro中的示例的主要内容,如果未能解决你的问题,请参考以下文章

通过在其上拖放文件或文件夹来启动 Swift OSX 应用程序

Swift OSX NSImageView 拖放

在同一行操场上打印字符 swift 3 [重复]

Swift Playground 支持 UIKit 吗?

如何在 OSX 上拖放期间检测 META 按键

计时器不在 Swift 3.0 操场上运行