Help us understand the problem. What is going on with this article?

[macOS] [Swift3.0] Drag & Dropでファイルパスを取得

More than 1 year has passed since last update.

2017/08/07 追記
[Swift][Cocoa] Drag&Drop したファイルのファイルパスを NSTextFeild に表示する
別のサンプル(こっちの swift のほうが良質かもしれない)

【OSX】【swift】Drag & Dropを受け付ける(ファイルパスを取得)
若干古かったのでSwift3.0で使えるものを。

AcceptDragView.swift
import Cocoa
import Foundation // containsに必要だった

class AcceptDragView : NSView {

    required init?(coder : NSCoder) {
        super.init(coder: coder)
        self.register(forDraggedTypes: [NSFilenamesPboardType])
    }

    override func draggingEntered(_ sender: NSDraggingInfo) -> NSDragOperation {
        return NSDragOperation.generic
    }

    override func performDragOperation(_ sender: NSDraggingInfo) -> Bool {
        var pboard : NSPasteboard! = sender.draggingPasteboard()
        if pboard != nil {
            pboard = sender.draggingPasteboard()

            let elements = pboard.types
            if (elements?.contains((NSFilenamesPboardType as NSString) as String))! {
                let files : [String] = pboard.propertyList(forType: NSFilenamesPboardType) as! [String]


                // print()がNSView.print()で印刷しようとしやがるのでNSTextFieldに出力するよ↓

                // let label = self.viewWithTag(1) as! NSTextField
                // label.stringValue = ""
                // for file in files {
                //     label.stringValue += file + "\n"
                // }
            }

            return true
        }
        return false
    }
}

正直まだswiftよく分かってないからif(elements?...の多重キャストが必要か分からん。

どうでもいいけどNSTextFieldのことUILabelって書きそうになるね。iOS病だね。

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした