NSUserdefaults用法总结
2018-07-20 来源:open-open
UserInfoManager.h文件 #import <UIKit/UIKit.h> @interface UserInfoManager : NSObject +(BOOL)initWithDic:(NSDictionary *)dic; +(NSDictionary *)readUserInfo; +(BOOL)isFirstLoad; +(BOOL)updateWithObject:(id)obj forKey:(NSString *)key; +(BOOL)updateWithBool:(BOOL)boolValue forKey:(NSString *)key; +(BOOL)updateWithFloat:(float)floatValue forKey:(NSString *)key; @end UserInfoManager.m文件 #import "UserInfoManager.h" #import "Foundation/Foundation.h" @interface UserInfoManager () @end @implementation UserInfoManager //是否第一次登陆 +(BOOL)isFirstLoad { if([[NSUserDefaults standardUserDefaults] objectForKey:@"isFirstLoad"]) { [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"isFirstLoad"]; return YES; } else { return NO; } } //创建一个NSUserDefaults对象用以保存数据 +(BOOL)initWithDic:(NSDictionary *)dic { NSUserDefaults * userInfoManager = [NSUserDefaults standardUserDefaults]; if (dic == Nil) { return NO; } NSArray * keys = [dic allKeys]; for(int i = 0;i < [keys count];i ++) { [userInfoManager setObject:[dic objectForKey:[keys objectAtIndex:i]] forKey:[keys objectAtIndex:i]]; } return [userInfoManager synchronize]; } //读取数据 +(NSDictionary *)readUserInfo { NSUserDefaults * userInfoManager = [NSUserDefaults standardUserDefaults]; return [userInfoManager dictionaryRepresentation]; } //更改某项数据 +(BOOL)updateWithObject:(id)obj forKey:(NSString *)key { [[NSUserDefaults standardUserDefaults] setObject:obj forKey:key]; return [[NSUserDefaults standardUserDefaults] synchronize]; } +(BOOL)updateWithBool:(BOOL)boolValue forKey:(NSString *)key { [[NSUserDefaults standardUserDefaults] setBool:boolValue forKey:key]; return [[NSUserDefaults standardUserDefaults] synchronize]; } +(BOOL)updateWithFloat:(float)floatValue forKey:(NSString *)key { [[NSUserDefaults standardUserDefaults] setFloat:floatValue forKey:key]; return [[NSUserDefaults standardUserDefaults] synchronize]; } @end
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点!
本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。
下一篇: .NET实现注册发送邮件激活账户
最新资讯
热门推荐