From 5948306016d833454f77f3743f177ab14d2a2ff8 Mon Sep 17 00:00:00 2001 From: Kyle Kendall Date: Sat, 3 Aug 2019 22:28:18 -0600 Subject: [PATCH 1/4] return optional url and remove need to pass in type explicitly --- Disk.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist | 8 ++++++++ Sources/Disk+Codable.swift | 8 +++++--- Sources/Disk+Data.swift | 4 +++- Sources/Disk+UIImage.swift | 4 +++- Sources/Disk+[Data].swift | 4 +++- Sources/Disk+[UIImage].swift | 5 ++++- 6 files changed, 26 insertions(+), 7 deletions(-) create mode 100644 Disk.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist diff --git a/Disk.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist b/Disk.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist new file mode 100644 index 0000000..18d9810 --- /dev/null +++ b/Disk.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist @@ -0,0 +1,8 @@ + + + + + IDEDidComputeMac32BitWarning + + + diff --git a/Sources/Disk+Codable.swift b/Sources/Disk+Codable.swift index 930046f..a028395 100644 --- a/Sources/Disk+Codable.swift +++ b/Sources/Disk+Codable.swift @@ -31,7 +31,8 @@ public extension Disk { /// - path: file location to store the data (i.e. "Folder/file.json") /// - encoder: custom JSONEncoder to encode value /// - Throws: Error if there were any issues encoding the struct or writing it to disk - static func save(_ value: T, to directory: Directory, as path: String, encoder: JSONEncoder = JSONEncoder()) throws { + @discardableResult + static func save(_ value: T, to directory: Directory, as path: String, encoder: JSONEncoder = JSONEncoder()) throws -> URL? { if path.hasSuffix("/") { throw createInvalidFileNameForStructsError() } @@ -40,6 +41,7 @@ public extension Disk { let data = try encoder.encode(value) try createSubfoldersBeforeCreatingFile(at: url) try data.write(to: url, options: .atomic) + return url } catch { throw error } @@ -132,14 +134,14 @@ public extension Disk { /// - decoder: custom JSONDecoder to decode existing values /// - Returns: decoded structs of data /// - Throws: Error if there were any issues retrieving the data or decoding it to the specified type - static func retrieve(_ path: String, from directory: Directory, as type: T.Type, decoder: JSONDecoder = JSONDecoder()) throws -> T { + static func retrieve(_ path: String, from directory: Directory, decoder: JSONDecoder = JSONDecoder()) throws -> T { if path.hasSuffix("/") { throw createInvalidFileNameForStructsError() } do { let url = try getExistingFileURL(for: path, in: directory) let data = try Data(contentsOf: url) - let value = try decoder.decode(type, from: data) + let value = try decoder.decode(T.self, from: data) return value } catch { throw error diff --git a/Sources/Disk+Data.swift b/Sources/Disk+Data.swift index 5a14fbf..d2a2a26 100644 --- a/Sources/Disk+Data.swift +++ b/Sources/Disk+Data.swift @@ -30,11 +30,13 @@ public extension Disk { /// - directory: user directory to store the file in /// - path: file location to store the data (i.e. "Folder/file.mp4") /// - Throws: Error if there were any issues writing the given data to disk - static func save(_ value: Data, to directory: Directory, as path: String) throws { + @discardableResult + static func save(_ value: Data, to directory: Directory, as path: String) throws -> URL? { do { let url = try createURL(for: path, in: directory) try createSubfoldersBeforeCreatingFile(at: url) try value.write(to: url, options: .atomic) + return url } catch { throw error } diff --git a/Sources/Disk+UIImage.swift b/Sources/Disk+UIImage.swift index 6ad3550..d030516 100644 --- a/Sources/Disk+UIImage.swift +++ b/Sources/Disk+UIImage.swift @@ -31,7 +31,8 @@ public extension Disk { /// - directory: user directory to store the image file in /// - path: file location to store the data (i.e. "Folder/file.png") /// - Throws: Error if there were any issues writing the image to disk - static func save(_ value: UIImage, to directory: Directory, as path: String) throws { + @discardableResult + static func save(_ value: UIImage, to directory: Directory, as path: String) throws -> URL? { do { var imageData: Data if path.suffix(4).lowercased() == ".png" { @@ -97,6 +98,7 @@ public extension Disk { let url = try createURL(for: path, in: directory) try createSubfoldersBeforeCreatingFile(at: url) try imageData.write(to: url, options: .atomic) + return url } catch { throw error } diff --git a/Sources/Disk+[Data].swift b/Sources/Disk+[Data].swift index a3b149f..51f55fc 100644 --- a/Sources/Disk+[Data].swift +++ b/Sources/Disk+[Data].swift @@ -30,7 +30,8 @@ public extension Disk { /// - directory: user directory to store the files in /// - path: folder location to store the data files (i.e. "Folder/") /// - Throws: Error if there were any issues creating a folder and writing the given [Data] to files in it - static func save(_ value: [Data], to directory: Directory, as path: String) throws { + @discardableResult + static func save(_ value: [Data], to directory: Directory, as path: String) throws -> URL? { do { let folderUrl = try createURL(for: path, in: directory) try createSubfoldersBeforeCreatingFile(at: folderUrl) @@ -41,6 +42,7 @@ public extension Disk { let dataUrl = folderUrl.appendingPathComponent(dataName, isDirectory: false) try data.write(to: dataUrl, options: .atomic) } + return folderUrl } catch { throw error } diff --git a/Sources/Disk+[UIImage].swift b/Sources/Disk+[UIImage].swift index e4f2410..e3c26aa 100644 --- a/Sources/Disk+[UIImage].swift +++ b/Sources/Disk+[UIImage].swift @@ -31,7 +31,8 @@ public extension Disk { /// - directory: user directory to store the images in /// - path: folder location to store the images (i.e. "Folder/") /// - Throws: Error if there were any issues creating a folder and writing the given images to it - static func save(_ value: [UIImage], to directory: Directory, as path: String) throws { + @discardableResult + static func save(_ value: [UIImage], to directory: Directory, as path: String) throws -> URL? { do { let folderUrl = try createURL(for: path, in: directory) try createSubfoldersBeforeCreatingFile(at: folderUrl) @@ -71,10 +72,12 @@ public extension Disk { } let imageUrl = folderUrl.appendingPathComponent(imageName, isDirectory: false) try imageData.write(to: imageUrl, options: .atomic) + return imageUrl } } catch { throw error } + return nil } /// Append an image to a folder From 88962da3f5a90a6764c985de94f7c11f9f0b60a9 Mon Sep 17 00:00:00 2001 From: Kyle Kendall Date: Sat, 3 Aug 2019 22:38:24 -0600 Subject: [PATCH 2/4] Remove checks plist --- Disk.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist | 8 -------- 1 file changed, 8 deletions(-) delete mode 100644 Disk.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist diff --git a/Disk.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist b/Disk.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist deleted file mode 100644 index 18d9810..0000000 --- a/Disk.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist +++ /dev/null @@ -1,8 +0,0 @@ - - - - - IDEDidComputeMac32BitWarning - - - From 14d56ee0e415890f077c6834930f31a1124ed470 Mon Sep 17 00:00:00 2001 From: Kyle Kendall Date: Sat, 3 Aug 2019 22:39:58 -0600 Subject: [PATCH 3/4] Remove change in type --- Sources/Disk+Codable.swift | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Sources/Disk+Codable.swift b/Sources/Disk+Codable.swift index a028395..ff7f93c 100644 --- a/Sources/Disk+Codable.swift +++ b/Sources/Disk+Codable.swift @@ -134,14 +134,14 @@ public extension Disk { /// - decoder: custom JSONDecoder to decode existing values /// - Returns: decoded structs of data /// - Throws: Error if there were any issues retrieving the data or decoding it to the specified type - static func retrieve(_ path: String, from directory: Directory, decoder: JSONDecoder = JSONDecoder()) throws -> T { + static func retrieve(_ path: String, from directory: Directory, as type: T.Type, decoder: JSONDecoder = JSONDecoder()) throws -> T { if path.hasSuffix("/") { throw createInvalidFileNameForStructsError() } do { let url = try getExistingFileURL(for: path, in: directory) let data = try Data(contentsOf: url) - let value = try decoder.decode(T.self, from: data) + let value = try decoder.decode(type, from: data) return value } catch { throw error From e6a6bff710469c4d40f83cf3349c4e8447cebff7 Mon Sep 17 00:00:00 2001 From: Kyle Kendall Date: Sun, 4 Aug 2019 00:10:13 -0600 Subject: [PATCH 4/4] Add correct return values --- Sources/Disk+Codable.swift | 2 +- Sources/Disk+Data.swift | 2 +- Sources/Disk+UIImage.swift | 2 +- Sources/Disk+[Data].swift | 6 ++++-- Sources/Disk+[UIImage].swift | 7 ++++--- 5 files changed, 11 insertions(+), 8 deletions(-) diff --git a/Sources/Disk+Codable.swift b/Sources/Disk+Codable.swift index ff7f93c..42b345a 100644 --- a/Sources/Disk+Codable.swift +++ b/Sources/Disk+Codable.swift @@ -32,7 +32,7 @@ public extension Disk { /// - encoder: custom JSONEncoder to encode value /// - Throws: Error if there were any issues encoding the struct or writing it to disk @discardableResult - static func save(_ value: T, to directory: Directory, as path: String, encoder: JSONEncoder = JSONEncoder()) throws -> URL? { + static func save(_ value: T, to directory: Directory, as path: String, encoder: JSONEncoder = JSONEncoder()) throws -> URL { if path.hasSuffix("/") { throw createInvalidFileNameForStructsError() } diff --git a/Sources/Disk+Data.swift b/Sources/Disk+Data.swift index d2a2a26..15908df 100644 --- a/Sources/Disk+Data.swift +++ b/Sources/Disk+Data.swift @@ -31,7 +31,7 @@ public extension Disk { /// - path: file location to store the data (i.e. "Folder/file.mp4") /// - Throws: Error if there were any issues writing the given data to disk @discardableResult - static func save(_ value: Data, to directory: Directory, as path: String) throws -> URL? { + static func save(_ value: Data, to directory: Directory, as path: String) throws -> URL { do { let url = try createURL(for: path, in: directory) try createSubfoldersBeforeCreatingFile(at: url) diff --git a/Sources/Disk+UIImage.swift b/Sources/Disk+UIImage.swift index d030516..e4550ee 100644 --- a/Sources/Disk+UIImage.swift +++ b/Sources/Disk+UIImage.swift @@ -32,7 +32,7 @@ public extension Disk { /// - path: file location to store the data (i.e. "Folder/file.png") /// - Throws: Error if there were any issues writing the image to disk @discardableResult - static func save(_ value: UIImage, to directory: Directory, as path: String) throws -> URL? { + static func save(_ value: UIImage, to directory: Directory, as path: String) throws -> URL { do { var imageData: Data if path.suffix(4).lowercased() == ".png" { diff --git a/Sources/Disk+[Data].swift b/Sources/Disk+[Data].swift index 51f55fc..d4d1ac5 100644 --- a/Sources/Disk+[Data].swift +++ b/Sources/Disk+[Data].swift @@ -31,18 +31,20 @@ public extension Disk { /// - path: folder location to store the data files (i.e. "Folder/") /// - Throws: Error if there were any issues creating a folder and writing the given [Data] to files in it @discardableResult - static func save(_ value: [Data], to directory: Directory, as path: String) throws -> URL? { + static func save(_ value: [Data], to directory: Directory, as path: String) throws -> [URL] { do { let folderUrl = try createURL(for: path, in: directory) try createSubfoldersBeforeCreatingFile(at: folderUrl) try FileManager.default.createDirectory(at: folderUrl, withIntermediateDirectories: false, attributes: nil) + var urls: [URL] = [] for i in 0.. URL? { + static func save(_ value: [UIImage], to directory: Directory, as path: String) throws -> [URL] { do { let folderUrl = try createURL(for: path, in: directory) try createSubfoldersBeforeCreatingFile(at: folderUrl) try FileManager.default.createDirectory(at: folderUrl, withIntermediateDirectories: false, attributes: nil) + var urls: [URL] = [] for i in 0..