To build on the Folderitem I posted yesterday, creating the SpecialFolder feature is ver simple…
/*
+------------+----------------------------------------------------------------+
| Name | SpecialFolderClass |
+------------+----------------------------------------------------------------+
| Type | class |
+------------+----------------------------------------------------------------+
| Properties| ApplicationSupport Resources |
| | Documents Temporary |
+------------+----------------------------------------------------------------+
*/
class SpecialFolderClass {
var Documents : folderitem {
get {return folderitem(path: fileMANAGER.urls(for: .documentDirectory, in: .userDomainMask)[0] ) }
}
var ApplicationSupport : folderitem {
get {
let path=folderitem(path: fileMANAGER.urls(for: .applicationSupportDirectory, in: .userDomainMask)[0] )
// this folder may not exist... so create it the first time user refers to it
if path.exists==false { path.CreateAsFolder() }
return path
}
}
var Resources : folderitem {
// Note : this folder is WRITEABLE on the iOS Simulator, but NOT on a real Device
get { return folderitem(path:mainBundle.resourceURL!) }
}
var Temporary : folderitem {
get { return folderitem(path:NSURL(string: NSTemporaryDirectory())! as URL) }
}
}
Also there are a few shared methods that Folderitem and future postings will require
import Foundation
let mainBundle : Bundle = Bundle.main
let fileMANAGER = FileManager.default
let SpecialFolder = SpecialFolderClass()
enum fileERRORS : Int {
case OK = 0
case RENAME = 1001
case INDEX = 1002
case COPY = 1003
case MOVE = 1004
case DIRECTORY = 1005
case DELETE = 1006
case TEXTOUT = 1007
case BADFILE = 1008
}
/*
+------------+----------------------------------------------------------------+
| Name | FileManager |
+------------+----------------------------------------------------------------+
| Type | extension |
+------------+----------------------------------------------------------------+
| Functions| isDirectoryAtPath |
+------------+----------------------------------------------------------------+
*/
extension FileManager {
func isDirectoryAtPath(_ url: URL) -> Bool {
// we need an Objective-C boolean
var isDirectory: ObjCBool = ObjCBool(false)
// determine if this is a directory
self.fileExists(atPath: url.relativePath, isDirectory: &isDirectory)
// convert to a Swift Boolean
let isDirectoryBool = isDirectory.boolValue
// return the Swift boolean
return isDirectoryBool
}
}
private func ThrowErrorMessage(_ errCode : fileERRORS,_ errorMsg:String="") {
var msg : String="?"
switch errCode {
case .OK : msg="OK"
case .RENAME : msg="Unable to RENAME file."
case .INDEX : msg="Index out of range"
case .COPY : msg="Unable to Copy file."
case .MOVE : msg="Unable to Move file."
case .DIRECTORY : msg="Unable to create directory."
case .DELETE : msg="Unable to delete file."
case .TEXTOUT : msg="No open file for write."
case .BADFILE : msg="Unable to access file."
}
zLastErrorCode = errCode.rawValue
zLastError = (zLastErrorCode != 0)
zLastErrorMsg = msg
if zLastError { print("ERROR : \(msg) [\(errorMsg)]") }
}