1
1
mirror of https://github.com/qvacua/vimr.git synced 2024-12-22 05:01:50 +03:00
vimr/VimR/FoundationCommons.swift

70 lines
1.6 KiB
Swift
Raw Normal View History

/**
* Tae Won Ha - http://taewon.de - @hataewon
* See LICENSE
*/
import Foundation
2016-09-25 18:50:33 +03:00
extension URL {
2016-09-07 21:12:18 +03:00
2016-10-08 20:58:21 +03:00
func isParent(of url: URL) -> Bool {
2016-09-25 18:50:33 +03:00
guard self.isFileURL && url.isFileURL else {
2016-09-07 21:12:18 +03:00
return false
}
2016-09-25 18:50:33 +03:00
let myPathComps = self.pathComponents
let targetPathComps = url.pathComponents
2016-09-07 21:12:18 +03:00
guard targetPathComps.count > myPathComps.count else {
return false
}
return Array(targetPathComps[0..<myPathComps.count]) == myPathComps
}
2016-11-12 18:42:10 +03:00
var parent: URL {
if self.path == "/" {
return self
}
return self.deletingLastPathComponent()
}
/// Wrapper function for NSURL.getResourceValue for Bool values.
/// Returns also `false` when
/// - there is no value for the given `key` or
/// - the value cannot be converted to `NSNumber`.
///
/// - parameters:
/// - key: The `key`-parameter of `NSURL.getResourceValue`.
2016-09-25 18:50:33 +03:00
func resourceValue(_ key: String) -> Bool {
var rsrc: AnyObject?
do {
2016-09-25 18:50:33 +03:00
try (self as NSURL).getResourceValue(&rsrc, forKey: URLResourceKey(rawValue: key))
} catch let error as NSError {
// FIXME error handling
print("\(#function): \(self) -> ERROR while getting \(key): \(error)")
return false
}
if let result = rsrc as? NSNumber {
return result.boolValue
}
return false
}
2016-11-12 18:42:10 +03:00
var isDir: Bool {
2016-09-25 18:50:33 +03:00
return self.resourceValue(URLResourceKey.isDirectoryKey.rawValue)
}
2016-11-12 18:42:10 +03:00
var isHidden: Bool {
2016-09-25 18:50:33 +03:00
return self.resourceValue(URLResourceKey.isHiddenKey.rawValue)
}
2016-11-12 18:42:10 +03:00
var isPackage: Bool {
2016-09-25 18:50:33 +03:00
return self.resourceValue(URLResourceKey.isPackageKey.rawValue)
}
}