As of this writing, NSFileManager unfortunately doesn't have a Swift-native way of determining if the object at an NSURL is a file or a directory. You can call the NSFileManager fileExistsAtPath:isDirectory:
function, but the isDirectory
parameter takes a reference to an Objective-C boolean, NOT a Swift-native boolean.
Extensions to the rescue! We can encapsulate our directory-testing logic into an NSFileManager
extension that returns a Swift-native Bool value, so we don't have to play with any Objective-C in our code.
//
// NSFileManager+CKIsDirectory.swift
//
import Foundation
extension NSFileManager {
func isDirectoryAtPath(url: NSURL) -> Bool {
// we need an Objective-C boolean
var isDirectory: ObjCBool = ObjCBool(false)
// determine if this is a directory
self.fileExistsAtPath(url.relativePath!, isDirectory: &isDirectory)
// convert to a Swift Boolean
let isDirectoryBool = Bool(isDirectory)
// return the Swift boolean
return isDirectoryBool
}
}