Last active
November 1, 2022 05:24
-
-
Save ANSCoder/5bd0b8de8a6d4ad4338f0ff027609a24 to your computer and use it in GitHub Desktop.
Read and Write Codable Models from the JSON file inside FileManager. π
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
// | |
// FileUtility.swift | |
// TravelApp | |
// | |
// Created by Anand Nimje on 13/04/20. | |
// Copyright Β© 2020 Anand. All rights reserved. | |
// | |
import Foundation | |
enum FileUtility<Value: Codable>{ | |
/// Add Files Name here | |
enum Files: CustomStringConvertible { | |
case airport | |
case hotelist | |
var description: String{ | |
switch self { | |
case .airport: | |
return "AirportList" | |
case .hotelist: | |
return "HotelCityList" | |
} | |
} | |
} | |
case fileName(Files) | |
} | |
extension FileUtility { | |
@discardableResult | |
func saveValue(_ model: Value) -> Bool{ | |
guard case .fileName(let file) = self else { | |
return false | |
} | |
do { | |
return try FileUtility.save(jsonObject: model.self, toFilename: file.description) | |
} catch { | |
return false | |
} | |
} | |
var loadValue: Value?{ | |
guard case .fileName(let file) = self else { | |
return nil | |
} | |
do { | |
return try FileUtility.loadJSON(withFilename: file.description) as? Value | |
} catch { | |
return nil | |
} | |
} | |
} | |
/// Uses | |
/// Fetch Model | |
/// | |
/// `let files = FileUtility<[AirPortCity]>.fileName(.airport).loadFile` | |
/// | |
/// Save Model | |
/// | |
/// `FileUtility<[AirPortCity]>.fileName(.airport).saveValue(values.airportCity ?? [])` | |
/// | |
//MARK: - Decoder & Encoder operations | |
private extension FileUtility{ | |
static func loadJSON(withFilename filename: String) throws -> Any? { | |
let fileManager = FileManager.default | |
let urls = fileManager.urls(for: .documentDirectory, in: .userDomainMask) | |
if let url = urls.first { | |
var fileURL = url.appendingPathComponent(filename) | |
fileURL = fileURL.appendingPathExtension("json") | |
let data = try Data(contentsOf: fileURL) | |
let jsonObject = try JSONDecoder().decode(Value.self, from: data) | |
return jsonObject | |
} | |
return nil | |
} | |
static func save(jsonObject: Value, toFilename filename: String) throws -> Bool{ | |
let fileManager = FileManager.default | |
let urls = fileManager.urls(for: .documentDirectory, in: .userDomainMask) | |
if let url = urls.first { | |
var fileURL = url.appendingPathComponent(filename) | |
fileURL = fileURL.appendingPathExtension("json") | |
let data = try JSONEncoder().encode(jsonObject) | |
//Checking file is available or now | |
guard fileManager.fileExists(atPath: fileURL.path) else { | |
/// Create new file here | |
return fileManager.createFile(atPath: fileURL.path, contents: data, attributes: nil) | |
} | |
try data.write(to: fileURL, options: [.atomicWrite]) | |
return true | |
} | |
return false | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment