检查文件是否为别名Swift [英] Check if file is alias Swift

查看:89
本文介绍了检查文件是否为别名Swift的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How can I check if a file is an Alias on Mac? Here is my code so far:

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

如何检查文件的名称和别名?

How can I check if the file is and alias?

推荐答案

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

Update: I initially mistakenly assumed that the only option is to use a CoreFoundation (C API) method, but that's not actually true (thanks): the Foundation (ObjC API) class NSURL does provide a way to detect Finder aliases:

// 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的方法如下:


[Not recommended, except as an exercise in using UnsafeMutablePointer<Void>]
Here's how to do it with the C-based CoreFoundation API:

  • IsAliasFile()在OS X 10.4中已弃用,
  • FSIsAliasFile()成功,在10.8中已弃用.
  • 当前方法是使用CFURLCopyResourcePropertyForKey(),在Swift中处理它并不有趣,因为必须在UnsafeMutablePointer<Void>中使用手动内存管理.
  • IsAliasFile() was deprecated in OS X 10.4,
  • succeeded by FSIsAliasFile(), which was deprecated in 10.8.
  • The current method is to use CFURLCopyResourcePropertyForKey(), which isn't fun to deal with in Swift, due to having to use manual memory management with UnsafeMutablePointer<Void>.

我希望我对内存的管理正确无误:

I hope I got the memory management right:

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
}

这篇关于检查文件是否为别名Swift的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆