GVUserDefaults - NSUserDefaults access via properties
GVUserDefaults是一款第三方库插件,可以直接用属性的方式来获取存储的值
Usage 使用方法
Create a category on GVUserDefaults
, add some properties in the .h file and make them @dynamic
in the .m file.
创建一个基于GVUserDefaults的category,将你需要使用的属性写在.h文件中,然后在.m文件中写上@dynamic与属性名即可.
// .h
@interface GVUserDefaults (Properties)
@property (nonatomic, weak) NSString *userName;
@property (nonatomic, weak) NSNumber *userId;
@property (nonatomic) NSInteger integerValue;
@property (nonatomic) BOOL boolValue;
@property (nonatomic) float floatValue;
@end
// .m
@implementation GVUserDefaults (Properties)
@dynamic userName;
@dynamic userId;
@dynamic integerValue;
@dynamic boolValue;
@dynamic floatValue;
@end
Now, instead of using [[NSUserDefaults standardUserDefaults] objectForKey:@"userName"]
, you can simply use [GVUserDefaults standardUserDefaults].userName
.
现在,可以不再使用[[NSUserDefaults standardUserDefaults] objectForKey:@"userName"]
,而是这么使用:[GVUserDefaults standardUserDefaults].userName
.
You can even save defaults by setting the property:
你甚至可以直接用以下的setter方法来设置属性(你写在分类中的)值:
[GVUserDefaults standardUserDefaults].userName = @"myusername"
;
Key prefix 键值前缀
The keys in NSUserDefaults are the same name as your properties. If you'd like to prefix or alter them, add a transformKey:
method to your category. For example, to turn "userName" into "NSUserDefaultUserName":
NSUserDefaults中的键值是的名字与你分类中属性名字是一样的,如果你想要去加一个前缀或者标记他们,使用 transformKey:
(这样可以使第一个字母大写并且加上NSUserDefault前缀)
- (NSString *)transformKey:(NSString *)key {
key = [key stringByReplacingCharactersInRange:NSMakeRange(0,1) withString:[[key substringToIndex:1] uppercaseString]];
return [NSString stringWithFormat:@"NSUserDefault%@", key];
}
Registering defaults 初始化 defaults
Registering defaults can be done as usual, on NSUserDefaults directly (use the same prefix, if any!).
初始化defaults 一样可以完成.你可以用以下方式来初始化.
NSDictionary *defaults = @{
@"NSUserDefaultUserName": @"default",
@"NSUserDefaultUserId": @1,
@"NSUserDefaultBoolValue": @YES
};
[[NSUserDefaults standardUserDefaults] registerDefaults:defaults];
However, it's a lot easier to create a setupDefaults method on the category, which takes care of the transformed keys automatically:
当然,直接在分类中创建一个 setupDefaults 方法,但是要小心自动转换的键值名字
- (NSDictionary *)setupDefaults {
return @{
@"userName": @"default",
@"userId": @1,
@"boolValue": @YES
};
}
NSUserDefaults initWithSuitName support (评论中说是plist的名字)
Simply create a methods called suitName
in your category and return the suitName you wish to use:
你只需在你的category中创建一个方法叫suitName,然后根据你的意愿返回你想要的值:
- (NSString *)suitName {
return @"com.example.mySuitName";
}
Performance
Performance is nearly identical to using NSUserDefaults directly. We're talking about a difference of 0.05 milliseconds or less.
性能的话完全不用担心,几乎跟NSUserDefaults使用一模一样.(作者在里面使用了时间戳对性能测试,性能还是不错的,跟系统自带的差不多)
Install
Install via CocoaPods (pod 'GVUserDefaults'
) or drag the code in the GVUserDefaults subfolder to your project.
你可以通过 CocoaPods 来安装,或者是将GVUserDefaults拖到你的项目当中.
Issues and questions
Have a bug? Please create an issue on GitHub!