Delete all keys from a NSUserDefaults dictionary iOS

If you have a look at the NSUserDefaults documentation you will see a method – (NSDictionary *) dictionaryRepresentation. Using this method on the standard user defaults, you can get a list of all keys in the user defaults. You can then use this to clear the user defaults: – (void)resetDefaults { NSUserDefaults * defs = … Read more

Save Struct to UserDefaults

In Swift 4 this is pretty much trivial. Make your struct codable simply by marking it as adopting the Codable protocol: struct Song:Codable { var title: String var artist: String } Now let’s start with some data: var songs: [Song] = [ Song(title: “Title 1”, artist: “Artist 1”), Song(title: “Title 2”, artist: “Artist 2”), Song(title: … Read more

Is there a way to get all values in NSUserDefaults? [duplicate]

Objective C all values: NSLog(@”%@”, [[[NSUserDefaults standardUserDefaults] dictionaryRepresentation] allValues]); all keys: NSLog(@”%@”, [[[NSUserDefaults standardUserDefaults] dictionaryRepresentation] allKeys]); all keys and values: NSLog(@”%@”, [[NSUserDefaults standardUserDefaults] dictionaryRepresentation]); using for: NSArray *keys = [[[NSUserDefaults standardUserDefaults] dictionaryRepresentation] allKeys]; for(NSString* key in keys){ // your code here NSLog(@”value: %@ forKey: %@”,[[NSUserDefaults standardUserDefaults] valueForKey:key],key); } Swift all values: print(UserDefaults.standard.dictionaryRepresentation().values) all keys: print(UserDefaults.standard.dictionaryRepresentation().keys) … Read more

Saving custom Swift class with NSCoding to UserDefaults

In Swift 4 or higher, Use Codable. In your case, use following code. class Blog: Codable { var blogName: String? } Now create its object. For example: var blog = Blog() blog.blogName = “My Blog” Now encode it like this: if let encoded = try? JSONEncoder().encode(blog) { UserDefaults.standard.set(encoded, forKey: “blog”) } and decode it like … Read more

Attempt to insert non-property list object when trying to save a custom object in Swift 3

You need to create Data instance from your JobCategory model using JSONEncoder and store that Data instance in UserDefaults and later decode using JSONDecoder. struct JobCategory: Codable { let id: Int let name: String } // To store in UserDefaults if let encoded = try? JSONEncoder().encode(category) { UserDefaults.standard.set(encoded, forKey: UserDefaultsKeys.jobCategory.rawValue) } // Retrieve from UserDefaults … Read more

how to save and read array of array in NSUserdefaults in swift?

The question reads “array of array” but I think most people probably come here just wanting to know how to save an array to UserDefaults. For those people I will add a few common examples. String array Save array let array = [“horse”, “cow”, “camel”, “sheep”, “goat”] let defaults = UserDefaults.standard defaults.set(array, forKey: “SavedStringArray”) Retrieve … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)