代表团(迅速)。或者:做一个快速的例子

时间:2016-02-18 19:42:50

标签: swift macos delegation

我试图在swift中的OSX下实现一个小文件系统观察器。我在Apple页面上找到了以下代码: (https://developer.apple.com/library/prerelease/ios/samplecode/Lister/Listings/Swift_ListerKit_DirectoryMonitor_swift.html

/*
    Copyright (C) 2016 Apple Inc. All Rights Reserved.
    See LICENSE.txt for this sample’s licensing information

    Abstract:
    `DirectoryMonitor` is used to monitor the contents of the provided directory by using a GCD dispatch source.
*/

import Foundation

/// A protocol that allows delegates of `DirectoryMonitor` to respond to changes in a directory.
protocol DirectoryMonitorDelegate: class {
    func directoryMonitorDidObserveChange(directoryMonitor: DirectoryMonitor)
}

class DirectoryMonitor {
    // MARK: Properties

    /// The `DirectoryMonitor`'s delegate who is responsible for responding to `DirectoryMonitor` updates.
    weak var delegate: DirectoryMonitorDelegate?

    /// A file descriptor for the monitored directory.
    var monitoredDirectoryFileDescriptor: CInt = -1

    /// A dispatch queue used for sending file changes in the directory.
    let directoryMonitorQueue = dispatch_queue_create("com.example.apple-samplecode.lister.directorymonitor", DISPATCH_QUEUE_CONCURRENT)

    /// A dispatch source to monitor a file descriptor created from the directory.
    var directoryMonitorSource: dispatch_source_t?

    /// URL for the directory being monitored.
    var URL: NSURL

    // MARK: Initializers
    init(URL: NSURL) {
        self.URL = URL
    }

    // MARK: Monitoring

    func startMonitoring() {
        // Listen for changes to the directory (if we are not already).
        if directoryMonitorSource == nil && monitoredDirectoryFileDescriptor == -1 {
            // Open the directory referenced by URL for monitoring only.
            monitoredDirectoryFileDescriptor = open(URL.path!, O_EVTONLY)

            // Define a dispatch source monitoring the directory for additions, deletions, and renamings.
            directoryMonitorSource = dispatch_source_create(DISPATCH_SOURCE_TYPE_VNODE, UInt(monitoredDirectoryFileDescriptor), DISPATCH_VNODE_WRITE, directoryMonitorQueue)

            // Define the block to call when a file change is detected.
            dispatch_source_set_event_handler(directoryMonitorSource!) {
                // Call out to the `DirectoryMonitorDelegate` so that it can react appropriately to the change.
                self.delegate?.directoryMonitorDidObserveChange(self)

                return
            }

            // Define a cancel handler to ensure the directory is closed when the source is cancelled.
            dispatch_source_set_cancel_handler(directoryMonitorSource!) {
                close(self.monitoredDirectoryFileDescriptor)

                self.monitoredDirectoryFileDescriptor = -1

                self.directoryMonitorSource = nil
            }

            // Start monitoring the directory via the source.
            dispatch_resume(directoryMonitorSource!)
        }
    }

    func stopMonitoring() {
        // Stop listening for changes to the directory, if the source has been created.
        if directoryMonitorSource != nil {
            // Stop monitoring the directory via the source.
            dispatch_source_cancel(directoryMonitorSource!)
        }
    }
}

现在我试图将这段代码变成一个有效的,最小的例子,但是我被困了,无法找到开头。具体来说,我不熟悉这里的授权概念(即使我想知道代表团做了什么)。

有人可以帮我把这段代码变成一个有效的例子吗?我一开始想做的就是观察文件夹' / tmp'并打印我们所有变更文件的名称。

谢谢! 诺贝特

0 个答案:

没有答案