[翻译] GVUserDefaults
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的值?是不是想直接用属性的方式来获取存储的值?
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:即可:
- (NSString *)transformKey:(NSString *)key {
key = [key stringByReplacingCharactersInRange:NSMakeRange(0,1) withString:[[key substringToIndex:1] uppercaseString]];
return [NSString stringWithFormat:@"NSUserDefault%@", key];
}
Registering defaults
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
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!
有bug?在GitHub的issue上提出来吧!
Contributing
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.
License
GVUserDefaults is available under the MIT license. See the LICENSE file for more info.
Thanks
A huge thank you goes to ADVUserDefaults for its method of creating accessors for primitive types.
首先,非常感谢 ADVUserDefaults ,我从里面获取到了非常多的灵感^_^!
[翻译] GVUserDefaults的更多相关文章
- 《Django By Example》第五章 中文 翻译 (个人学习,渣翻)
书籍出处:https://www.packtpub.com/web-development/django-example 原作者:Antonio Melé (译者@ucag注:大家好,我是新来的翻译, ...
- 《Django By Example》第四章 中文 翻译 (个人学习,渣翻)
书籍出处:https://www.packtpub.com/web-development/django-example 原作者:Antonio Melé (译者注:祝大家新年快乐,这次带来<D ...
- [翻译]开发文档:android Bitmap的高效使用
内容概述 本文内容来自开发文档"Traning > Displaying Bitmaps Efficiently",包括大尺寸Bitmap的高效加载,图片的异步加载和数据缓存 ...
- 【探索】机器指令翻译成 JavaScript
前言 前些时候研究脚本混淆时,打算先学一些「程序流程」相关的概念.为了不因太枯燥而放弃,决定想一个有趣的案例,可以边探索边学. 于是想了一个话题:尝试将机器指令 1:1 翻译 成 JavaScript ...
- 《Django By Example》第三章 中文 翻译 (个人学习,渣翻)
书籍出处:https://www.packtpub.com/web-development/django-example 原作者:Antonio Melé (译者注:第三章滚烫出炉,大家请不要吐槽文中 ...
- 《Django By Example》第二章 中文 翻译 (个人学习,渣翻)
书籍出处:https://www.packtpub.com/web-development/django-example 原作者:Antonio Melé (译者注:翻译完第一章后,发现翻译第二章的速 ...
- 《Django By Example》第一章 中文 翻译 (个人学习,渣翻)
书籍出处:https://www.packtpub.com/web-development/django-example 原作者:Antonio Melé (译者注:本人目前在杭州某家互联网公司工作, ...
- 【翻译】Awesome R资源大全中文版来了,全球最火的R工具包一网打尽,超过300+工具,还在等什么?
0.前言 虽然很早就知道R被微软收购,也很早知道R在统计分析处理方面很强大,开始一直没有行动过...直到 直到12月初在微软技术大会,看到我软的工程师演示R的使用,我就震惊了,然后最近在网上到处了解和 ...
- ASP.NET MVC with Entity Framework and CSS一书翻译系列文章之第一章:创建基本的MVC Web站点
在这一章中,我们将学习如何使用基架快速搭建和运行一个简单的Microsoft ASP.NET MVC Web站点.在我们马上投入学习和编码之前,我们首先了解一些有关ASP.NET MVC和Entity ...
随机推荐
- 2014年度辛星html教程夏季版第一节
从今天起开始在博客园开启自己的html教程啦,先从第一节开始把,首先推荐一个网站,就是http:/www.w3cschool.cc,这是一个公开的教学网站,但是它有一个问题,那就是虽然很全面,但是不是 ...
- PHP利器-WAMPServer
为了配置memcacheAdmin,牵涉到搭建PHP的环境,使用的是WAMPServer,安装之后,需要对apache进行配置, 端口,需要将wamp\bin\apache\Apache2.2.21\ ...
- iOS开发之Runloop(转)
Objective-C之run loop详解 作者:wangzz 原文地址:http://blog.csdn.net/wzzvictory/article/details/9237973 转载请注明出 ...
- 数据库之--- SQLite 语句
一. 基础创表操作: 1. 创建表 CREATE TABLE IF NOT EXISTS t_dog(name text, age bolb, weight real); 2. 插入记录 INSERT ...
- java 常用基本数据类型的默认值
在使用基本数据类型作为类成员的时候,有时只初始化了而没有给变量赋值,那么此时,java会给你的变量赋一个默认初始值. boolean false char '/ ...
- hdu 4731
一道找规律的题,但今天的智商捉急,一直都想不到点子上: 比赛之后和别人讨论的时候,在n=2的情况下,前面两个是aa,后面就接着很多个aababb,然后最后再判断下就行了~~~ 以后对于这种题还是不要太 ...
- 【整理】各种Python的IDE(集成开发环境)的总结和对比
原地址:http://www.tuicool.com/articles/rMVJNn 原文 http://www.crifan.com/summary_common_python_ide_pyscr ...
- 自己写的一个android小应用 手电筒
简洁 小巧 免费 无广告 代码奉上 active类 private boolean isopent = false; private Camera camera; @Override protecte ...
- pdfminer的TextConverter得到文件字符无空格解决方法
from pdfminer.pdfinterp import PDFResourceManager, process_pdf from pdfminer.converter import TextCo ...
- 17.1.1.6 Creating a Data Snapshot Using Raw Data Files 创建一个数据快照使用 Raw Data Files
17.1.1.6 Creating a Data Snapshot Using Raw Data Files 创建一个数据快照使用 Raw Data Files 如果数据库是大的, 复制raw 数据文 ...