💾 LMStorage is a lightweight Swift framework that simplifies persistent storage and leverages the power of Codable for seamless data serialization and deserialization. It reduces boilerplate and streamlines the management of your app’s data layer.
- iOS 15.0+
- tvOS 15.0+
- Swift 5.0+
LMStorage is available through SPM. To install it, follow the steps:
Open Xcode project > File > Swift Packages > Add Package Dependecy
After that, put the url in the field: https://github.com/thejohnlima/LMStorage.git
LMStorage is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod'LMStorage'and run pod install
{
"title": "Iron Man",
"category": "action",
"year": 2008,
"rate": "94%",
"image": "https://i.pinimg.com/564x/9d/e9/1e/9de91e58cfde7f05eb79e203301980ef.jpg"
}import LMStorage
structMovie:LMCodable{lettitle:String?letdescription:String?letcategory:String?letyear:Int?letrate:String?letlink:String?letimage:String?}Parsing the JSON movie using a Data type returned by the request
letmovie=Movie(data)Parsing a local JSON file
letmovie:Movie?=Movie.getItem(from:"file_name")[
{
"title": "Iron Man",
"category": "action",
"year": 2008,
"rate": "94%",
"image": "https://i.pinimg.com/564x/9d/e9/1e/9de91e58cfde7f05eb79e203301980ef.jpg"
},
{
"title": "Black Panther",
"category": "action",
"year": 2018,
"rate": "96%",
"image": "https://i.pinimg.com/564x/43/cd/5b/43cd5b065b271006da5491645e0564c3.jpg"
}
]import LMStorage
structMovie:LMCodable{lettitle:String?letdescription:String?letcategory:String?letyear:Int?letrate:String?letlink:String?letimage:String?}Parsing the JSON movie using a Data type returned by the request
letdata:Data=...letmovies:[Movie]= data.toItems()Parsing a local JSON file
letmovies:[Movie]=Movie.getItems(from:"movies_file_name")Import library in your file:
import LMStorageCreate a struct for your Defaults files:
structMyDefaults:LMDefaults{enumKeys:String{case currentUser
case accessToken
case haveSeenOnboarding
}}Than, in the view controller just save what you need:
classViewController:UIViewController{overridefunc viewDidLoad(){
super.viewDidLoad()MyDefaults.set(true, forKey:.haveSeenOnboarding)lethaveSeenOnboarding=MyDefaults.bool(forKey:.haveSeenOnboarding)print("Have Seen Onboarding: \(haveSeenOnboarding)")}}Import library in your file:
import LMStorage/// Saving user example
letuser=User(id:"1", name:"John", age:30)letstorage=UserStorage()
storage.create(user)/// Saving secure user example
letuser=User(id:"2", name:"Test", age:1130)letsecureStorage=UserSecureStorage()
secureStorage.create(user)/// Storage Keys Example
structKey{staticletcontainer="LMStorage"staticletuser="User"}/// User Example
structUser:LMCodable{letid:Stringletname:Stringletage:Int}/// User storage example
structUserStorage:LMStorageProtocol{typealiasT=Userprivateletstorage:LMAbstractStorage<T>init(){
storage =LMStorage(with:Key.user)}func getFirst()->User?{return storage.getFirst()}func create(_ register:User)->Bool{return storage.create(register)}func update(_ register:User)->Bool{return storage.update(register)}func delete()->Bool{return storage.delete()}}/// User secure storage example
structUserSecureStorage:LMStorageProtocol{typealiasT=Userprivateletstorage=LMSecureStorage<T>(with:Key.container)init(){}func getFirst()->User?{return storage.getFirst(key:Key.user)}func create(_ register:User)->Bool{return storage.create(register, key:Key.user)}func update(_ register:User)->Bool{return storage.update(register, key:Key.user)}func delete()->Bool{return storage.delete(key:Key.user)}}If you need more examples, take a look at Example project.
- If you found a bug, open an issue.
- If you have a feature request, open an issue.
- If you want to contribute, submit a pull request. 👨🏻💻
LMStorage is under MIT license. See the LICENSE file for more info.
