CodableFiles - Save and load Codable objects from DocumentDirectory on iOS Devices.

Overview

“CodableFiles”

CocoaPods Carthage Swift Package Manager

Welcome to CodableFiles, a simple library that provides an easier way to save, load or delete Codable objects in Documents directory. It’s primarily aimed to save Encodable objects as json string and loads back from string to Decodable object. It's essentially a thin wrapper around the FileManager APIs that Foundation provides.

Features

  • Modern, object-oriented API for accessing, reading and writing files.
  • Unified, simple do, try, catch error handling.
  • Easily to find and interact with saved files.
  • Unit tests for most of the use cases.

Examples

Codable object

struct User: Codable {
    let name: String
    let lastName: String
}

CodableFiles shared reference.

let codableFiles = CodableFiles.shared

Save Codable object in default directory.

let user = User(name: "First name", lastName: "Last name")
let savePath = try? codableFiles.save(object: user, withFilename: "userModel")

Load Codable object from default directory.

let loadedObject = try? codableFiles.load(objectType: User.self, withFilename: "userModel")

Save array of Codable objects in default directory.

let user = User(name: "First name", lastName: "Last name")
let anotherUser = User(name: "Another first name", lastName: "Another last name")
let savePath = try? codableFiles.saveAsArray(objects: [user, anotherUser], withFilename: "usersArray")

Load array of Codable objects from default directory.

let loadedObjects = try? codableFiles.loadAsArray(objectType: User.self, withFilename: "usersArray")

Load Codable object from a file that is inside app bundle.

if let filePath = Bundle.main.path(forResource: "user", ofType: "json"),
    let filePathUrl = URL(string: bundle) {
    let loadedObject = try? codableFiles.load(objectType: User.self, atPath: filePathUrl)
}

Delete a file from default directory.

try? codableFiles.deleteFile(withFileName: "userModel")

Delete a file from given directory.

try? codableFiles.deleteFile(withFileName: "userModel", atDirectory: "directoryName")

Delete default directory.

try? codableFiles.deleteDirectory()

Delete a directory.

try? codableFiles.deleteDirectory(directoryName: "directoryName")

App bundle

AppBundle is Read-only, so you can not write anything to it programmatically. That's the reason we are using Documents Directory always to read & write data. Read more: https://developer.apple.com/library/archive/documentation/FileManagement/Conceptual/FileSystemProgrammingGuide/FileSystemOverview/FileSystemOverview.html

Installation

CocoaPods

CocoaPods is a dependency manager for Cocoa projects. For usage and installation instructions, visit their website. To integrate CodableFiles into your Xcode project using CocoaPods, specify it in your Podfile:

pod 'CodableFiles'

Carthage

Carthage is a decentralized dependency manager that builds your dependencies and provides you with binary frameworks. To integrate CodableFiles into your Xcode project using Carthage, specify it in your Cartfile:

github "egzonpllana/CodableFiles"

Swift Package Manager through Manifest File

The Swift Package Manager is a tool for automating the distribution of Swift code and is integrated into the swift compiler.

Once you have your Swift package set up, adding CodableFiles as a dependency is as easy as adding it to the dependencies value of your Package.swift.

dependencies: [
    .package(url: "https://github.com/egzonpllana/CodableFiles.git", .upToNextMajor(from: "0.1.7"))
]

Swift Package Manager through XCode

To add CodableFiles as a dependency to your Xcode project, select File > Swift Packages > Add Package Dependency and enter the repository URL

https://github.com/egzonpllana/CodableFiles.git

As a file

Since all of CodableFiles is implemented within a single file, you can easily use it in any project by simply dragging the file CodableFiles.swift into your Xcode project.

Backstory

So, why was this made? While I was working on a project to provide mocked URL sessions with dynamic JSON data, I found that we can have these data saved in a file in Document Directory or loaded from Bundle so later we can update, read or delete based on our app needs. The objects that have to be saved or loaded must conform to the Codable protocol. So, I made Codable Files that make it possible to work with JSON data quicker, in an expressive way.

Questions or feedback?

Feel free to open an issue, or find me @egzonpllana on LinkedIn.

You might also like...
Synco - Synco uses Firebase's Realtime Database to synchronize data across multiple devices, in real time

Synco Synco uses Firebase's Realtime Database to synchronize a color across mult

Prephirences is a Swift library that provides useful protocols and convenience methods to manage application preferences, configurations and app-state. UserDefaults
Prephirences is a Swift library that provides useful protocols and convenience methods to manage application preferences, configurations and app-state. UserDefaults

Prephirences - Preϕrences Prephirences is a Swift library that provides useful protocols and convenience methods to manage application preferences, co

Classes-and-structures-in-swift - This source files show what is the difference between class and structure

This source files show what is the difference between class and structure You ca

Disk is a powerful and simple file management library built with Apple's iOS Data Storage Guidelines in mind
Disk is a powerful and simple file management library built with Apple's iOS Data Storage Guidelines in mind

Disk is a powerful and simple file management library built with Apple's iOS Data Storage Guidelines in mind

Simple, Strongly Typed UserDefaults for iOS, macOS and tvOS
Simple, Strongly Typed UserDefaults for iOS, macOS and tvOS

简体中文 DefaultsKit leverages Swift 4's powerful Codable capabilities to provide a Simple and Strongly Typed wrapper on top of UserDefaults. It uses less

An efficient, small mobile key-value storage framework developed by WeChat. Works on Android, iOS, macOS, Windows, and POSIX.
An efficient, small mobile key-value storage framework developed by WeChat. Works on Android, iOS, macOS, Windows, and POSIX.

中文版本请参看这里 MMKV is an efficient, small, easy-to-use mobile key-value storage framework used in the WeChat application. It's currently available on Andr

A demo app to showcase pixel perfect, modern iOS development with SwiftUI and Combine on MVVM-C architecture.

Pixel_Perfect_SwiftUI A demo app to showcase pixel perfect, modern iOS development with SwiftUI and Combine on MVVM-C architecture. Tech Stack: Swift,

xFonts is an app that lets you install custom fonts on iOS and iPadOS.

xFonts xFonts is an app that lets you install custom fonts on iOS and iPadOS. Grab fonts from your iCloud Drive or Dropbox and add them to your collec

Comments
  • use `NSData.WritingOptions.atomic` instead

    use `NSData.WritingOptions.atomic` instead

    I have an issue on compile my project with CodableFiles 1.0.1 as a dependency in swift docker 5.6 and this PR fixes it by using NSData.WritingOptions.atomic instead of .atomicWrite on write file since.atomicWrite has been marked as deprecated.

    Thanks for your cool project 🤜

    opened by frank89722 1
Releases(1.0.2)
Owner
Egzon Pllana
Passionate iOS Developer
Egzon Pllana
Store and retrieve Codable objects to various persistence layers, in a couple lines of code!

tl;dr You love Swift's Codable protocol and use it everywhere, who doesn't! Here is an easy and very light way to store and retrieve Codable objects t

null 149 Dec 15, 2022
Save-the-dot-project-swift - Save the dot project with swift

Save the Dot Apple introduced UIViewPropertyAnimator for iOS 10. We can use this

Kushal Shingote 2 Feb 8, 2022
Why not use UserDefaults to store Codable objects 😉

tl;dr You love Swift's Codable protocol and use it everywhere, who doesn't! Here is an easy and very light way to store and retrieve -reasonable amoun

Omar Albeik 452 Oct 17, 2022
Modern interface to UserDefaults + Codable support

Default Modern interface to UserDefaults + Codable support What is Default? Default is a library that extends what UserDefaults can do by providing ex

Nicholas Maccharoli 475 Dec 20, 2022
AppCodableStorage - Extends `@AppStorage` in SwiftUI to support any Codable object

AppCodableStorage Extends @AppStorage in SwiftUI to support any Codable object.

Andrew Pouliot 19 Nov 25, 2022
MoreCodable expands the possibilities of `Codable`.

MoreCodable MoreCodable expands the possibilities of "Codable". Installation Carthage github "tattn/MoreCodable" CocoaPods pod 'MoreCodable' Feature D

Tatsuya Tanaka 372 Dec 7, 2022
Typed key-value storage solution to store Codable types in various persistence layers with few lines of code!

?? Stores A typed key-value storage solution to store Codable types in various persistence layers like User Defaults, File System, Core Data, Keychain

Omar Albeik 94 Dec 31, 2022
Save NSObject into NSUserDefaults in one-line, auto class mapping

Akaibu What is it ? Archive any class in just ONE-LINE of code. Automatically map class's properties under the hood. Drop in replacement of NSObject S

Roy Tang 16 Jun 21, 2021
ObjectBox Swift - persisting your Swift objects superfast and simple

ObjectBox Swift ObjectBox is a superfast, light-weight object persistence framework. This Swift API seamlessly persists objects on-device for iOS and

ObjectBox 380 Dec 19, 2022
A Generic CoreData Manager to accept any type of objects. Fastest way for adding a Database to your project.

QuickDB FileManager + CoreData ❗️ Save and Retrieve any thing in JUST ONE line of code ❗️ Fast usage dataBase to avoid struggling with dataBase comple

Behrad Kazemi 17 Sep 24, 2022