iphaaw
iphaaw

Reputation: 7204

Determine a directory is a package in swift

How can I tell if a file is a package rather than a directory?

var isDir = ObjCBool(false)
let exists = NSFileManager.defaultManager().fileExistsAtPath(path, isDirectory: &isDir)

return isDir.boolValue

This code returns true for both directories and packages.

Upvotes: 1

Views: 276

Answers (1)

Jeremy
Jeremy

Reputation: 4381

There’s a Cocoa method, isFilePackageAtPath, on NSWorkspace for that.

import Cocoa

let sw = NSWorkspace.sharedWorkspace()

if sw.isFilePackageAtPath("/Applications/Xcode.app") {
    // True, so this will execute.
}
if sw.isFilePackageAtPath("/usr/bin") {
    // False, so this won't execute.
}

Upvotes: 4

Related Questions