检查文件是否是别名Swift

时间:2015-10-26 02:45:33

标签: macos swift nsfilemanager

如何检查文件是否是Mac上的别名?到目前为止,这是我的代码:

public func getFiles(){
    let folderPath = "/Users/timeBro/Desktop/testfolder"
    let fileManager = NSFileManager.defaultManager()
    let enumerator:NSDirectoryEnumerator = fileManager.enumeratorAtPath(folderPath)!

    for url in enumerator.allObjects {

        let newurl = NSURL(string: url as! String)
        print("\(url as! String)")
        print(url);
        print(newurl?.isFileReferenceURL())
    }
}

如何检查文件是否为别名?

2 个答案:

答案 0 :(得分:2)

更新:我最初错误地认为唯一的选择是使用 CoreFoundation (C API)方法,但实际上并非如此(thanks) : Foundation (ObjC API)类NSURL确实提供了一种检测Finder别名的方法:

// OSX 10.9+
// Indicates if the specified filesystem path is a Finder alias.
// Returns an optional Boolean: if the lookup failed, such when the path doesn't exist,
// nil is returned.
// Example: isFinderAlias("/path/to/an/alias")
func isFinderAlias(path:String) -> Bool? {
    let aliasUrl = NSURL(fileURLWithPath: path)
    var isAlias:AnyObject? = nil
    do {
        try aliasUrl.getResourceValue(&isAlias, forKey: NSURLIsAliasFileKey)
    } catch _ {}
    return isAlias as! Bool?
}

[不建议,除非是使用UnsafeMutablePointer<Void>] 的练习 以下是使用基于C的CoreFoundation API的方法:

  • IsAliasFile()在OS X 10.4中已弃用,
  • FSIsAliasFile()继承,已在10.8中弃用。
  • 目前的方法是使用CFURLCopyResourcePropertyForKey(),由于必须使用UnsafeMutablePointer<Void>进行手动内存管理,因此在Swift中处理起来并不好玩。

我希望我的内存管理正确:

import Foundation

// Indicates if the specified filesystem path is a Finder alias.
// Returns an optional Boolean: if the lookup failed, such when the path
// doesn't exist, nil is returned.
// Example: isFinderAlias("/path/to/an/alias")
func isFinderAlias(path:String) -> Bool? {

    var isAlias:Bool? = nil // Initialize result var.

    // Create a CFURL instance for the given filesystem path.
    // This should never fail, because the existence isn't verified at this point.
    // Note: No need to call CFRelease(fUrl) later, because Swift auto-memory-manages CoreFoundation objects.
    let fUrl = CFURLCreateWithFileSystemPath(nil, path, CFURLPathStyle.CFURLPOSIXPathStyle, false)

    // Allocate void pointer - no need for initialization,
    // it will be assigned to by CFURLCopyResourcePropertyForKey() below.
    let ptrPropVal = UnsafeMutablePointer<Void>.alloc(1)

    // Call the CoreFoundation function that copies the desired information as
    // a CFBoolean to newly allocated memory that prt will point to on return.
    if CFURLCopyResourcePropertyForKey(fUrl, kCFURLIsAliasFileKey, ptrPropVal, nil) {

        // Extract the Bool value from the memory allocated.
        isAlias = UnsafePointer<CFBoolean>(ptrPropVal).memory as Bool

        // Since the CF*() call contains the word "Copy", WE are responsible
        // for destroying (freeing) the memory.
        ptrPropVal.destroy()
    }

    // Deallocate the pointer
    ptrPropVal.dealloc(1)

    return isAlias
}

答案 1 :(得分:2)

有一个简单的解决方案可以在没有任何指针处理的情况下完全实现:

extension URL {
    func isAlias() -> Bool? {
        let values = try? url.resourceValues(forKeys: [.isSymbolicLinkKey, .isAliasFileKey])
        let alias = values?.isAliasFile
        let symbolic = values?.isSymbolicLink

        guard alias != nil, symbolic != nil else { return nil }
        if alias! && !symbolic! {
            return true
        }
        return false
    }
}

说明:resourceValues(forKeys:)为符号链接返回.isAliasFile .isSymbolicLink,因此您必须确保返回前者而后者不是对于别名。 如果路径不存在,则函数返回nil。