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
| import Disk | |
| do { | |
| try Disk.save(posts, to: .documents, as: "posts.json") | |
| let retrieved = try Disk.retrieve("posts.json", from: .documents, as: [Post].self) | |
| } catch { | |
| // ... | |
| } |
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
| // Remove messages.json from Documents Directory | |
| Storage.remove("messages.json", from: .documents) | |
| // Or just clear the Documents Directory entirely | |
| Storage.clear(.documents) | |
| // We can even check if our file exists in the specified directory | |
| if Storage.fileExists("messages.json", in: .documents) { | |
| // we have messages to retrieve | |
| } |
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
| let messagesFromDisk = Storage.retrieve("messages.json", from: .documents, as: [Message].self) |
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
| var messages = [Message]() | |
| for i in 1...4 { | |
| let newMessage = Message(title: "Message \(i)", body: "...") | |
| messages.append(newMessage) | |
| } | |
| Storage.store(messages, to: .documents, as: "messages.json") |
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
| struct Message: Codable { | |
| let title: String | |
| let body: String | |
| } |
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
| import Foundation | |
| public class Storage { | |
| fileprivate init() { } | |
| enum Directory { | |
| // Only documents and other data that is user-generated, or that cannot otherwise be recreated by your application, should be stored in the <Application_Home>/Documents directory and will be automatically backed up by iCloud. | |
| case documents | |
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
| sampleImageView.addTapGestureRecognizer { | |
| print("image tapped") | |
| } |
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
| import UIKit | |
| extension UIView { | |
| // In order to create computed properties for extensions, we need a key to | |
| // store and access the stored property | |
| fileprivate struct AssociatedObjectKeys { | |
| static var tapGestureRecognizer = "MediaViewerAssociatedObjectKey_mediaViewer" | |
| } | |
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
| func getDocumentsURL() -> URL { | |
| if let url = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first { | |
| return url | |
| } else { | |
| fatalError("Could not retrieve documents directory") | |
| } | |
| } |
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
| let fileManager = FileManager() | |
| func savePostsToDisk(posts: [Post]) { | |
| // 1. Create a url for documents-directory/posts.json | |
| let url = getDocumentsURL().appendingPathComponent("posts.json") | |
| // 2. Endcode our [Post] data to JSON Data | |
| let encoder = JSONEncoder() | |
| do { | |
| let data = try encoder.encode(posts) | |
| // 3. Check if posts.json already exists... |