Swiftpack.co -  Package - nhnam/SafeUserDefaults
Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
nhnam/SafeUserDefaults
A Better UserDefaults with storing policy: override, discard
.package(url: "https://github.com/nhnam/SafeUserDefaults.git", from: "1.0.2")

SafeUserDefaults

A Better UserDefaults with storing policy: override, discard

// import module
import SafeModeUserDefault

Example 1: Save item using SafeMode

// Save item if not exist
let storingResult =  UserDefaults.safe.setValue("123456", forKey: "selected_number", forceOverride: { 
  return false
})
// return StoringResult.success

Example 2: Discard if there is an item exist

// Don't override if there is a value exist
let storingResult =  UserDefaults.safe.setValue("123456", forKey: "selected_number", forceOverride: { 
  return false
})
// return StoringResult.discarded

Example 3: Force overide existing value

let role  = Role.admin

// Force override set to true due a condition
let storingResult = UserDefaults.safe.setValue("123456", forKey: "selected_number", forceOverride: { 
  return role == .admin 
})
// return StoringResult.overrided

GitHub

link
Stars: 0
Last commit: 3 weeks ago

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco