IOS nsuserdefaults-lightweight Local data store
IOS offers a number of ways to handle user data persistence: nsuserdefaults, plist, and sqlite3 databases are great choices!
Nsuserdefaults is a lightweight local data store that is easy to operate, but only supports storage of six data types such as bool, Float, Nsinteger, Object, Double, URL
Nsuserdefaults provides methods for adding, reading, and removing, for developers to invoke, as shown in the following example:
GNSUserDefaults.h
#import <foundation/foundation.h>/** * gnsuserdefaults * Lightweight Local data store (nsuserdefaults) */@interface Gnsuserdefaults : nsobject/** * get_userdefaults * Get and Initialize nsuserdefaults Object * * @return nsuserdefaults */+ (nsuserdefaults *) Get_userde faults; #pragma mark-class userdefaults save data (only six types are supported: Bool, Float, Nsinteger, Object, Double, URL)/** * saveuserdefaults_ BOOL * Save Data * * @param objvalue BOOL saved data value * @param objkey nsstring saved Data key */+ (void) Saveuserdefaults_bool: (BOOL) OB Jvalue Objkey: (NSString *) objkey;/** * saveuserdefaults_float * Save Data * * @param objvalue Float saved data value * @param objkey NSString saved Data key */+ (void) Saveuserdefaults_float: (Float) ObjValue objkey: (NSString *) objkey;/** * saveuserdefaults_int Eger * Save Data * * @param objvalue float saved data value * @param objkey nsstring saved Data key */+ (void) Saveuserdefaults_integer: (Nsin Teger) ObjValue Objkey: (NSString *) objkey;/** * Saveuserdefaults_nsobject * Save Data * * @param objvalue nsobject saved data value * @param objkey nsstring saved Data key */+ (void) Saveuserdefaults_nsobject: (NSObject *) ObjValue Objkey: (NSString *) objkey;/** * saveuserdefaults_double * Save data * * @param objvalue double saved data value * @param objkey nsstring saved Data key */+ (void) Saveuserdefaults_double: (double) objvalue Objkey: (NSString *) objkey;/** * Saveuserdefaults_nsurl * Save Data * * @param objvalue nsurl Saved data value * @param objkey NSSt Ring-Saved Data key */+ (void) Saveuserdefaults_nsurl: (Nsurl *) ObjValue Objkey: (NSString *) Objkey; #pragma mark-class read Userdefaults data (only six types are supported: Bool, Float, Nsinteger, Object (ID), Double, URL)/** * Readuserdefaults_bool * Read Data * * @param objkey NSString Read Key Name * * @return nsobject key corresponding value */+ (BOOL) Readuserdefaults_bool: (NSString *) objkey;/** * Readuserdefaults_flo At * Read Data * * @param objkey nsstring Read Key name * * @return nsobject key corresponding value */+ (float) readuserdefaults_float: (NSString *) obj key;/** * Readuserdefaults_integer * Read Data * * @param objkey nsstring Read Key name * * @return nsobject key corresponding value */+ (Nsinteger) re Aduserdefaults_integer: (NSString *) objkey;/** * Readuserdefaults_object * Read Data * * @param objkey nsstring Read Key name * * @return nsobject key corresponding value */+ (ID) readus Erdefaults_object: (NSString *) objkey;/** * readuserdefaults_double * Read Data * * @param objkey nsstring Read Key name * * @retur n nsobject key corresponding value */+ (double) readuserdefaults_double: (NSString *) objkey;/** * Readuserdefaults_url * Read Data * * @param ob Jkey NSString Read Key name * * @return nsobject key corresponding value */+ (Nsurl *) Readuserdefaults_url: (NSString *) Objkey; @end
Gnsuserdefaults.m
#import "GNSUserDefaults.h"/** * gnsuserdefaults * Lightweight Local data storage (nsuserdefaults) */@implementation Gnsuserdefaults#pragma Mark-class static variablestatic nsuserdefaults* userdefaults; #pragma mark-class method+ (nsuserdefaults *) get_ userdefaults{if (!userdefaults) userdefaults = [Nsuserdefaults standarduserdefaults]; return userdefaults;} #pragma mark-class userdefaults save data (only six types are supported: Bool, Float, Nsinteger, Object, Double, URL) + (void) Saveuserdefaults_bool :(BOOL) ObjValue objkey: (NSString *) objkey{[[Gnsuserdefaults get_userdefaults] Setbool:objvalue ForKey:objKey]; It is recommended to store it synchronously on disk, but it is not necessary [[Gnsuserdefaults get_userdefaults] synchronize];} + (void) Saveuserdefaults_float: (Float) ObjValue objkey: (NSString *) objkey{[[Gnsuserdefaults Get_userdefaults] Setfloat:objvalue Forkey:objkey]; It is recommended to store it synchronously on disk, but it is not necessary [[Gnsuserdefaults get_userdefaults] synchronize];} + (void) Saveuserdefaults_integer: (Nsinteger) ObjValue objkey: (NSString *) objkey{[[Gnsuserdefaults Get_userDefAults] Setinteger:objvalue Forkey:objkey]; It is recommended to store it synchronously on disk, but it is not necessary [[Gnsuserdefaults get_userdefaults] synchronize];} + (void) Saveuserdefaults_nsobject: (nsobject*) ObjValue objkey: (NSString *) objkey{[[Gnsuserdefaults Get_userDefaults ] Setobject:objvalue Forkey:objkey]; It is recommended to store it synchronously on disk, but it is not necessary [[Gnsuserdefaults get_userdefaults] synchronize];} + (void) saveuserdefaults_double: (Double) ObjValue objkey: (NSString *) objkey{[[Gnsuserdefaults Get_userdefaults] Setdouble:objvalue Forkey:objkey]; It is recommended to store it synchronously on disk, but it is not necessary [[Gnsuserdefaults get_userdefaults] synchronize];} + (void) Saveuserdefaults_nsurl: (Nsurl *) ObjValue Objkey: (NSString *) objkey{[[Gnsuserdefaults Get_userdefaults] SetUR L:objvalue Forkey:objkey]; It is recommended to store it synchronously on disk, but it is not necessary [[Gnsuserdefaults get_userdefaults] synchronize];} #pragma mark-class read userdefaults data (only six types are supported: Bool, Float, Nsinteger, Object (ID), Double, URL) + (BOOL) readuserdefaults_ Bool: (NSString *) objkey{return [[Gnsuserdefaults Get_userdefaultS] boolforkey:objkey];} + (float) readuserdefaults_float: (NSString *) objkey{return [[Gnsuserdefaults get_userdefaults] floatforkey:objkey];} + (Nsinteger) Readuserdefaults_integer: (NSString *) objkey{return [[Gnsuserdefaults Get_userdefaults] IntegerForKey:o Bjkey];} + (ID) readuserdefaults_object: (NSString *) objkey{return [[Gnsuserdefaults get_userdefaults] objectforkey:objkey];} + (Double) readuserdefaults_double: (NSString *) objkey{return [[Gnsuserdefaults Get_userdefaults] Doubleforkey:objkey ];} + (Nsurl *) Readuserdefaults_url: (NSString *) objkey{return [[Gnsuserdefaults get_userdefaults] urlforkey:objkey];} @end
Removing objects: Removeobjectforkey
For more information, refer to: http://blog.csdn.net/enuola/article/details/7959767
IOS nsuserdefaults-lightweight Local data store