2015-10-26 95 views
1

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

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()) 
    } 
} 

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

+0

你是对的。它正在工作,但我会改变这一点。问题仍然是别名 – Silve2611

回答

2

更新:我最初错误地认为,唯一的选择是使用一个的CoreFoundation(C API)的方法,但实际上并不是真正的(thanks):在基金会(ObjC API)NSURL类并提供一种方法来检测搜索别名:

// 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(),这在Swift中不太好玩,因为必须使用手动内存管理和UnsafeMutablePointer<Void>

我希望我有足够的内存管理权:

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 
} 
+0

尼斯解决方案。在我发布另一个问题之前。我将如何获取别名指向的文档的文件路径? – Silve2611

+0

@ Silve2611:这更涉及到了,所以我建议你提出一个新问题;该文档中提到“首先使用CFURLCreateBookmarkDataFromFile,然后使用CFURLCreateByResolvingBookmarkData”。一旦你提出了新的问题,如果你仍然需要帮助,请随时通知我。 – mklement0

+0

好的,我会的。我也做了很大一部分,但我只得到Fileid不是路径。 – Silve2611

1

有一个简单的解决方案,它完全地没有任何指针处理:

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用于符号链接,因此您必须确保前者返回,而后者不检查别名时ES。 如果路径不存在,函数返回nil。