Android equivalent of NSUserDefaults in iOS

This is the most simple solution I’ve found: //–Init int myvar = 12; //–SAVE Data SharedPreferences preferences = context.getSharedPreferences(“MyPreferences”, Context.MODE_PRIVATE); SharedPreferences.Editor editor = preferences.edit(); editor.putInt(“var1”, myvar); editor.commit(); //–READ data myvar = preferences.getInt(“var1”, 0); Where ‘context’ is the current context (e.g. in an activity subclass could be this).

Why NSUserDefaults failed to save NSMutableDictionary in iOS?

I found out one alternative, before save, I encode the root object (NSArray object) using NSKeyedArchiver, which ends with NSData. Then use UserDefaults save the NSData. When I need the data, I read out the NSData, and use NSKeyedUnarchiver to convert NSData back to the object. It is a little cumbersome, because i need to … Read more

How can I use UserDefaults in Swift?

ref: NSUserdefault objectTypes Swift 3 and above Store UserDefaults.standard.set(true, forKey: “Key”) //Bool UserDefaults.standard.set(1, forKey: “Key”) //Integer UserDefaults.standard.set(“TEST”, forKey: “Key”) //setObject Retrieve UserDefaults.standard.bool(forKey: “Key”) UserDefaults.standard.integer(forKey: “Key”) UserDefaults.standard.string(forKey: “Key”) Remove UserDefaults.standard.removeObject(forKey: “Key”) Remove all Keys if let appDomain = Bundle.main.bundleIdentifier { UserDefaults.standard.removePersistentDomain(forName: appDomain) } Swift 2 and below Store NSUserDefaults.standardUserDefaults().setObject(newValue, forKey: “yourkey”) NSUserDefaults.standardUserDefaults().synchronize() Retrieve var returnValue: [NSString]? … Read more

What’s the optimum way of storing an NSDate in NSUserDefaults?

You are needlessly complicating things. Why are you converting the date to a time interval (then the time interval to a different primitive)? Just [sharedDefaults setObject:theDate forKey:@”theDateKey”] and be done with it. NSDate is one of the “main types” supported by the PLIST format (dates, numbers, strings, data, dictionaries, and arrays), so you can just … Read more

How to save local data in a Swift app?

The simplest solution for storing a few strings or common types is UserDefaults. The UserDefaults class provides convenience methods for accessing common types such as floats, doubles, integers, Boolean values, and URLs. UserDefaults lets us store objects against a key of our choice, It’s a good idea to store these keys somewhere accessible so we … Read more

Attempt to set a non-property-list object as an NSUserDefaults

The code you posted tries to save an array of custom objects to NSUserDefaults. You can’t do that. Implementing the NSCoding methods doesn’t help. You can only store things like NSArray, NSDictionary, NSString, NSData, NSNumber, and NSDate in NSUserDefaults. You need to convert the object to NSData (like you have in some of the code) … Read more

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