IOS local storage nsuserdefaults encapsulation code

Time:2020-8-1

Direct code


#import <Foundation/Foundation.h>
@interface NSUserDefaultUtil : NSObject
+(void)PutDefaults:(NSString *)key Value:(id)value;
+(id)GetDefaults:(NSString *)key;
@end

#import "NSUserDefaultUtil.h"
@implementation NSUserDefaultUtil
+(void)PutDefaults:(NSString *)key Value:(id)value{
 if (key!=NULL&&value!=NULL) {
  NSUserDefaults *userDefaults=[NSUserDefaults standardUserDefaults];
  [userDefaults setObject:value forKey:key];
 }
}
+(id)GetDefaults:(NSString *)key{
 NSUserDefaults *userDefaults=[NSUserDefaults standardUserDefaults];
 id obj;
 if (key!=NULL) {
  obj=[userDefaults objectForKey:key];
 }
 return obj;
}
@end

usage method


[NSUserDefaultUtil PutDefaults:@"key" Value:str];
self.str = [NSUserDefaultUtil GetDefaults:@"key"];

The above encapsulation code of IOS local storage nsuserdefaults is the whole content shared by Xiaobian. I hope it can give you a reference, and I hope you can support developeppaer more.

Recommended Today

MySQL indexing principle

Before we start, let’s introduce oneData structure visualization website, which can visually operate various data structures. Easy to understand the underlying principles. Meaning of index When we were in primary school, we all had the experience of using a dictionary. To find a word, we usually look it up on the index page in front […]