时间:2022-06-17 08:36:17 | 栏目:iOS代码 | 点击:次
直接上代码
#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
使用方法
[NSUserDefaultUtil PutDefaults:@"key" Value:str]; self.str = [NSUserDefaultUtil GetDefaults:@"key"];