Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 334 Vote(s) - 3.53 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Attempt to set a non-property-list object as an NSUserDefaults

#11
> Swift 5: The **Codable** protocol can be used instead of **NSKeyedArchiever**.

struct User: Codable {
let id: String
let mail: String
let fullName: String
}

The **Pref** struct is custom wrapper around the UserDefaults standard object.

struct Pref {
static let keyUser = "Pref.User"
static var user: User? {
get {
if let data = UserDefaults.standard.object(forKey: keyUser) as? Data {
do {
return try JSONDecoder().decode(User.self, from: data)
} catch {
print("Error while decoding user data")
}
}
return nil
}
set {
if let newValue = newValue {
do {
let data = try JSONEncoder().encode(newValue)
UserDefaults.standard.set(data, forKey: keyUser)
} catch {
print("Error while encoding user data")
}
} else {
UserDefaults.standard.removeObject(forKey: keyUser)
}
}
}
}

So you can use it this way:

Pref.user?.name = "John"

if let user = Pref.user {...

Reply

#12
**Swift** with `@propertyWrapper`

Save `Codable` object to `UserDefault`


@propertyWrapper
struct UserDefault<T: Codable> {
let key: String
let defaultValue: T

init(_ key: String, defaultValue: T) {
self.key = key
self.defaultValue = defaultValue
}

var wrappedValue: T {
get {

if let data = UserDefaults.standard.object(forKey: key) as? Data,
let user = try? JSONDecoder().decode(T.self, from: data) {
return user

}

return defaultValue
}
set {
if let encoded = try? JSONEncoder().encode(newValue) {
UserDefaults.standard.set(encoded, forKey: key)
}
}
}
}




enum GlobalSettings {

@UserDefault("user", defaultValue: User(name:"",pass:"")) static var user: User
}




Example User model confirm Codable

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

**How to use it**


//Set value
GlobalSettings.user = User(name: "Ahmed", pass: "Ahmed")

//GetValue
print(GlobalSettings.user)
Reply

#13
I ran into this and eventually figured out it was because I was trying to use `NSNumber` as dictionary keys, and property lists only allow strings as keys. The documentation for [`setObject:forKey:`](

[To see links please register here]

) doesn't mention this limitation, but the [About Property Lists](

[To see links please register here]

) page that it links to does:

> By convention, each Cocoa and Core Foundation object listed in Table 2-1 is called a property-list object. Conceptually, you can think of “property list” as being an abstract superclass of all these classes. If you receive a property list object from some method or function, you know that it must be an instance of one of these types, but a priori you may not know which type. If a property-list object is a container (that is, an array or dictionary), all objects contained within it must also be property-list objects. If an array or dictionary contains objects that are not property-list objects, then you cannot save and restore the hierarchy of data using the various property-list methods and functions. And although NSDictionary and CFDictionary objects allow their keys to be objects of any type, <b>if the keys are not string objects, the collections are not property-list objects.</b>

(Emphasis mine)
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through