nmdias / defaultskit Goto Github PK
View Code? Open in Web Editor NEWSimple, Strongly Typed UserDefaults for iOS, macOS and tvOS
License: MIT License
Simple, Strongly Typed UserDefaults for iOS, macOS and tvOS
License: MIT License
[Leak Checks]
538.14 KB 85.4% 9255 0x100829463 DefaultsKit
538.14 KB 85.4% 9255 JSONDecoder.decode(_:from:)
279.39 KB 44.3% 4772 +[NSJSONSerialization JSONObjectWithData:options:error:]
Dears:
I don't know how to read it back at different view controllers. For example: I save my data at login view controller, and I need to read it out at Friends View Controller. How do I do? Thanks!
Is there a swift3 version? Thanks.
When I tried to build this project with Xcode 14.3, the build failed.
I think this error occurred because the deployment target was not specified
First thanks for sharing this useful frame.
My problem is when I use Xcode 11.5 simulator iOS 10.3.1 test Defaults.shared.has(.userIdKey) function (.userIdKey is String type) after I use clear the value, it's always return true. But when I use Defaults.shared.get(for: .userIdKey) is nil.
So I debug has function source code "po userDefaults.value(forKey: key._key)" is โฟ Optional
(lldb) po userDefaults.object(forKey: key._key)
โฟ Optional
I test same code on iOS 11.4, iOS12, iOS13 on simulator are all correct.
I don't know what's make this issue. I have no real machine with version 10.3.1 to test it.
At last I use userDefaults.string(forKey: key._key) test it is correct, It seems userDefaults.value have problem on iOS 10.3.1 version.
May be public func has(_ key: Key) we can get value type first, then use match function get value is better.
Hello,
Awesome Library!!!
I am trying for the below things:
My Goal
defaults.set(true, forKey: .isUserLoggedIn)
Currently I'm doing in this way:
enum Keys {
static let isUserLoggedIn = Key<Bool>("isUserLoggedIn")
}
defaults.set(true, forKey: Keys.isUserLoggedIn)
Please guide me.
This was an accident when I converted the custom model using an archive to binary using "Defaults" for storage. This is a problem I recently used with Xcode 9.4.1, Swift 4.0. I didn't see this problem before using Xcode 9.2. The version of Defaults that I am using now is 0.0.8.
I also looked at this document "https://git.snooey.net/Mirrors/swift/blob/a8ba0772cd2f887c2b48549172d2c447140e06d8/stdlib/public/SDK/Foundation/JSONEncoder.swift"
I don't quite understand why this error caused my storage binary type to fail.
I hope you can help me.
Thanks
Hi!
Great idea/framework. Can't wait to use Codable everywhere in my projects.
Looked over the code and thought of something.
Maybe both could be an option. Just curious. ๐
I know this comment is annoying, I wouldn't normally raise for a nit but this wont work if in DEBUG.
Just noticed it in passing...
DefaultsKit/Sources/Defaults.swift
Line 92 in 53aa9b7
We have to define generic type of every Key instance but I do not get it.
first parameter of 'save' method 'value' already knows what generic type is gonna be saved.
Is it possible to improve Key class which is gonna not be required to define what generic type is?
What I mean:
let object = Person(n: "Mustafa")
Defaults.shared.set(object, Key("admin")
we can get the type of object variable.
Hi @nmdias
Can Key
class inherit a class like following, for example?
public class AnyKey {}
public final class Key<ValueType: Codable>: AnyKey {}
This makes it possible to do following.
extension AnyKey {
static let key = Key<String>("someKey")
}
defaults.set("Codable FTW ๐", for: .key)
My goal is like #9.
Thanks.
Testing out the repo, looks great! Of course, immediately set out to create default keys, as that just feels right. Noted the Defaults.swift file is missing (that is in the demo).
ps: naming the file to match only makes sense too ;)
Cheers!
One very useful feature when debugging is to use Arguments Passed On Launch to override user defaults. It seems DefaultsKit is not able to handle these keys because of the way it attempts to cast to Key.ValueType
.
For example, let's say I have the following:
var wipeDatabaseOnLaunch = Key<Bool>("wipeDatabaseOnLaunch")
This works in the normal case:
let value = defaults.get(for: wipeDatabaseOnLaunch)
// value: Optional<Bool>(true)
However, if we pass -wipeDatabaseOnLaunch true
argument on launch, the underlying type returned by user defaults is no longer Optional<Bool>
but instead Optional<Any>
. Since the implementation of get
tries to cast to the key's ValueType
this cast will fail and you get nil
instead.
let value = defaults.get(for: wipeDatabaseOnLaunch)
// value: nil
As a workaround I had to implement a specialized version of get
which uses bool(forKey:)
public func get(for key: Key<Bool>) -> Bool {
return userDefaults.bool(forKey: key._key)
}
Unfortunately, this needs to be done for each of the types you could use as launch arguments. Haven't found a cleaner solution to this.
I get this error when I try to install DefaultsKit with SPM (Carthage works)
because every version of DefaultsKit contains incompatible tools version and root depends on DefaultsKit 0.2.0, version solving failed.
Create a README.CN.md in Chinese.
Chinese is the #1 spoken language in the world and I'd love to have DefaultsKit be more inclusive, unfortunately I don't speak Chinese. If you know Chinese, and would like to help out, I'd be eternally grateful.
Thank you ๐
How do I store an array object in it
I need something like this
static let someKey = Key<String>("someKey", defaultValue: "Alex")
I have App where I use custom keyboard extension and I want to share default with group identifires for that I need to initialise default with suit name how can we do this with DefaultsKit ?
How can one store an array of complex objects?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.