IOS NSUserDefaults-lightweight local data storage

Source: Internet
Author: User

IOS NSUserDefaults-lightweight local data storage
IOS NSUserDefaults-lightweight local data storage

IOS provides multiple processing methods for user data persistence: NSUserDefaults, plist, and sqlite3 databases are all good choices!

NSUserDefaults is a lightweight local data storage and easy to operate. It only supports storage of Bool, Float, NSInteger, Object, Double, and Url data types.

NSUserDefaults provides adding, reading, and removing methods for developers to call. An example is as follows:

GNSUserDefaults. h
# Import
 
  
/*** GNSUserDefaults * lightweight local data storage (NSUserDefaults) */@ interface GNSUserDefaults: NSObject/*** get_userDefaults * Get and initialize the NSUserDefaults object ** @ return NSUserDefaults */+ (NSUserDefaults *) get_userDefaults; # pragma mark-Class UserDefaults (only six types are supported: Bool, Float, NSInteger, Object, Double, Url) /*** saveUserDefaults_Bool * save data ** @ param objValue BOOL save data value * @ param objKey NSString save data key */+ (void) saveUserDefaults_Bool :( BOOL) objValue objKey :( NSString *) objKey; /*** saveUserDefaults_Float * save data ** @ param objValue float save data value * @ param objKey NSString save data key */+ (void) saveUserDefaults_Float :( float) objValue objKey :( NSString *) objKey; /*** saveUserDefaults_Integer * save data ** @ param objValue float save data value * @ param objKey NSString save data key */+ (void) saveUserDefaults_Integer :( NSInteger) objValue objKey :( NSString *) objKey; /***** save data ** @ param objValue NSObject save data value * @ param objKey NSString save data key */+ (void) saveUserDefaults_NSObject :( NSObject *) objValue objKey :( NSString *) objKey; /*** saveUserDefaults_Double * save data ** @ param objValue double save data value * @ param objKey NSString save data key */+ (void) saveUserDefaults_Double :( double) objValue objKey :( NSString *) objKey; /*** saveUserDefaults_NSURL * save data ** @ param objValue NSURL save data value * @ param objKey NSString save data key */+ (void) saveUserDefaults_NSURL :( NSURL *) objValue objKey :( NSString *) objKey; # pragma mark-Class reads UserDefaults data (only the following types are supported: Bool, Float, NSInteger, Object (id), Double, and Url) /*** readUserDefaults_Bool * reads data ** @ param objKey NSString the name of the key to be read ** @ return NSObject the corresponding value */+ (BOOL) readUserDefaults_Bool :( NSString *) objKey; /*** readUserDefaults_Float * read data ** @ param objKey NSString the name of the key to be read ** @ return NSObject the corresponding value */+ (float) readUserDefaults_Float :( NSString *) objKey; /*** readUserDefaults_Integer * reads data ** @ param objKey NSString the name of the key to be read ** @ return NSObject the corresponding value */+ (NSInteger) readUserDefaults_Integer :( NSString *) objKey; /*** readUserDefaults_Object * reads data ** @ param objKey NSString the name of the key to be read ** @ return NSObject the corresponding value */+ (id) readUserDefaults_Object :( NSString *) objKey; /*** readUserDefaults_Double * read data ** @ param objKey NSString the name of the key to be read ** @ return NSObject the corresponding value */+ (double) readUserDefaults_Double :( NSString *) objKey; /*** readUserDefaults_URl * reads data ** @ param objKey NSString the name of the key to be read ** @ return NSObject the 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 stores data (only six types are supported: Bool, Float, NSInteger, Object, Double, Url) + (void) Upload :( BOOL) objValue objKey :( NSString *) objKey {[[GNSUserDefaults get_userDefaults] setBool: objValue forKey: objKey]; // we recommend that you synchronize the data to the disk, however, it is not necessary to [[synchronize get_userDefaults] synchronize];} + (void) Merge :( float) objValue objKey :( NSString *) objKey {[[GNSUserDefaults get_userDefaults] setFloat: objValue forKey: objKey]; // It is recommended that the data be stored to the disk synchronously, but not required [[GNSUserDefaults get_userDefaults] synchronize];} + (void) saveUserDefaults_Integer :( NSInteger) objValue objKey :( NSString *) objKey {[[GNSUserDefaults get_userDefaults] setInteger: objValue forKey: objKey]; // It is recommended that the data be stored in the disk synchronously, but not necessarily [[GNSUserDefaults get_userDefaults] synchronize];} + (void) Upload :( NSObject *) objValue objKey :( NSString *) objKey {[[GNSUserDefaults get_userDefaults] setObject: objValue forKey: objKey]; // we recommend that you store the object to the disk synchronously, but not required [[[GNSUserDefaults get_userDefaults] synchronize];} + (void) values :( double) objValue objKey :( NSString *) objKey {[[GNSUserDefaults get_userDefaults] setDouble: objValue forKey: objKey]; // It is recommended that the data be stored to the disk synchronously, but it is not necessary to [[GNSUserDefaults get_userDefaults] synchronize];} + (void) saveUserDefaults_NSURL :( NSURL *) objValue objKey :( NSString *) objKey {[[GNSUserDefaults get_userDefaults] setURL: objValue forKey: objKey]; // we recommend that you store the data to the disk synchronously, however, it is not necessary to [[GNSUserDefaults get_userDefaults] synchronize];} # pragma mark-Class to read UserDefaults data (only the following 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) returns :( NSString *) objKey {return [[GNSUserDefaults get_userDefaults] integerForKey: objKey];} + (id) readUserDefaults_Object :( NSString *) objKey {return [[GNSUserDefaults get_userDefaults] objectForKey: objKey];} + (double) readUserDefaults_Double :( NSString *) objKey {return [[Response get_userDefaults] doubleForKey: objKey];} + (NSURL *) returns :( NSString *) objKey {return [[GNSUserDefaults get_userDefaults] URLForKey: objKey];} @ end

Removed object: removeObjectForKey

 

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.