Skip to content

ytyubox/StateDefaults

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

23 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

StateDefaults

Swift Swift 5.1 SPM

Usage

struct ContentView {
  @StateDefaults("key", defaultValue: 0) var tapCount

  var body: some View {
  Button("tap \(tapCount)") {
    self.tapCount += 1
    }
  }
}

Using Custom UserDefaults

@StateDefaults("key", defaultValue: 0, userDefaults: myuserDefaults) var tapCount

Avoid Duplicate Key

let p1 = StateDefaults("someKey", defaultValue: 1, userDefaults: defaults)
let p2 = StateDefaults("someKey", defaultValue: 1, userDefaults: defaults)
// precondition  "[StateDefault] key: someKey is already a property somewhere in you code. Place make sure you have only one"

However, if the holder of StateDefaults can be destroy by arc/stack, it will be fine.

do {
  _ = StateDefaults("someKey", defaultValue: 1, userDefaults: defaults)
  }
	
  _ = StateDefaults("someKey", defaultValue: 1, userDefaults: defaults)
}
// work fine

Author

twitter : @YuTsungYu

License

MyLibrary is available under the MIT license. See the LICENSE file for more info.