GVUserDefaults
Tired of writing all that code to get and set defaults in NSUserDefaults? Want to have code completion and compiler checks by using properties instead?
是不是已經厭倦了設置NSUserDefaults的值?是不是想直接用屬性的方式來獲取存儲的值?
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";
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:即可:
- (NSString *)transformKey:(NSString *)key {
key = [key stringByReplacingCharactersInRange:NSMakeRange(0,1) withString:[[key substringToIndex:1] uppercaseString]];
return [NSString stringWithFormat:@"NSUserDefault%@", key];
}
Registering defaults can be done as usual, on NSUserDefaults directly (use the same prefix, if any!).
你可以用以下方式來恢復默認值.
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:
當然,你可以用下面更簡單的方式來恢復默認值^_^!
- (NSDictionary *)setupDefaults {
return @{
@"userName": @"default",
@"userId": @1,
@"boolValue": @YES
};
}
NSUserDefaults initWithSuitName support
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 is nearly identical to using NSUserDefaults directly. We're talking about a difference of 0.05 milliseconds or less.
性能的話完全不用擔心,幾乎跟NSUserDefaults使用一模一樣.
Install via CocoaPods (pod 'GVUserDefaults'
) or drag the code in the GVUserDefaults subfolder to your project.
你可以通過 CocoaPods 來安裝,或者是將GVUserDefaults拖到你的項目當中.
Have a bug? Please create an issue on GitHub!
有bug?在GitHub的issue上提出來吧!
GVUserDefaults is an open source project and your contribution is very much appreciated.
GVUserDefaults是一個開源的項目,非常歡迎你給這份源碼做貢獻.
- Check for open issues or open a fresh issue to start a discussion around a feature idea or a bug.
- Fork the repository on Github and make your changes on the develop branch (or branch off of it). Please retain the code style that is used in the project.
- Write tests, make sure everything passes.
- Send a pull request.
GVUserDefaults is available under the MIT license. See the LICENSE file for more info.
A huge thank you goes to ADVUserDefaults for its method of creating accessors for primitive types.
首先,非常感謝 ADVUserDefaults ,我從里面獲取到了非常多的靈感^_^!