Created
June 26, 2018 04:18
-
-
Save aybekckaya/1e5ada87f6331280c6a287673552fe19 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension String { | |
static var documentsPath:String { | |
let paths = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask) | |
let documentsDirectory = paths[0] | |
return documentsDirectory.path | |
} | |
var filePathAtDocumentsDirectory:String { | |
let docPath = String.documentsPath | |
return (docPath as NSString).appendingPathComponent(self) | |
} | |
var isFileExistsAtPath:Bool { | |
return FileManager.default.fileExists(atPath: self) | |
} | |
var listOfFilesAtPath:[String] { | |
do { | |
return try FileManager.default.contentsOfDirectory(atPath: self) | |
} catch { | |
print("Error while enumerating files \(error.localizedDescription)") | |
return [] | |
} | |
} | |
var readDataFromPlistPath:[String:Any]? { | |
guard self.isFileExistsAtPath == true else { return nil } | |
return NSDictionary(contentsOf: URL(fileURLWithPath: self) ) as? [String:Any] | |
} | |
func deleteFileAtPathIfExists() { | |
guard self.isFileExistsAtPath else { return } | |
try! FileManager.default.removeItem(atPath: self) | |
} | |
} | |
extension Dictionary { | |
func saveToPlist(plistPath:String) { | |
guard let dct = self as? [String:Any] else { return } | |
(dct as NSDictionary).write(toFile: plistPath, atomically: true) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment