1
1
mirror of https://github.com/qvacua/vimr.git synced 2024-12-23 05:32:13 +03:00
vimr/VimR/FoundationCommons.swift

62 lines
1.4 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
}
/// 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 {
// FIXME error handling
print("\(#function): \(self) -> ERROR while getting \(key)")
return false
}
if let result = rsrc as? NSNumber {
return result.boolValue
}
return false
}
var dir: Bool {
2016-09-25 18:50:33 +03:00
return self.resourceValue(URLResourceKey.isDirectoryKey.rawValue)
}
var hidden: Bool {
2016-09-25 18:50:33 +03:00
return self.resourceValue(URLResourceKey.isHiddenKey.rawValue)
}
var package: Bool {
2016-09-25 18:50:33 +03:00
return self.resourceValue(URLResourceKey.isPackageKey.rawValue)
}
}