Getting alias path of file in swift

前端 未结 4 1457
时光说笑
时光说笑 2020-12-03 19:18

I\'m having trouble resolving the alias link on mac. I\'m checking if the file is an alias and then I would want to receive the original path. Instead I\'m only getting a Fi

4条回答
  •  无人及你
    2020-12-03 19:42

    Here's a Swift 3 implementation, based largely on vadian's approach. My idea is to return a file URL, so I effectively combine it with fileURLWithPath. It's an NSURL class extension because I need to be able to call into it from existing Objective-C code:

    extension NSURL {
        class func fileURL(path:String, resolveAlias yn:Bool) -> URL {
            let url = URL(fileURLWithPath: path)
            if !yn {
                return url
            }
            do {
                let vals = try url.resourceValues(forKeys: [.isAliasFileKey])
                if let isAlias = vals.isAliasFile {
                    if isAlias {
                        let original = try URL(resolvingAliasFileAt: url)
                        return original
                    }
                }
            } catch {
                return url // give up
            }
            return url // really give up
        }
    }
    

提交回复
热议问题