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是一个开源的项目,非常欢迎你给这份源码做贡献.

  1. Check for open issues or open a fresh issue to start a discussion around a feature idea or a bug.
  2. 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.
  3. Write tests, make sure everything passes.
  4. 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的更多相关文章

  1. 《Django By Example》第五章 中文 翻译 (个人学习,渣翻)

    书籍出处:https://www.packtpub.com/web-development/django-example 原作者:Antonio Melé (译者@ucag注:大家好,我是新来的翻译, ...

  2. 《Django By Example》第四章 中文 翻译 (个人学习,渣翻)

    书籍出处:https://www.packtpub.com/web-development/django-example 原作者:Antonio Melé (译者注:祝大家新年快乐,这次带来<D ...

  3. [翻译]开发文档:android Bitmap的高效使用

    内容概述 本文内容来自开发文档"Traning > Displaying Bitmaps Efficiently",包括大尺寸Bitmap的高效加载,图片的异步加载和数据缓存 ...

  4. 【探索】机器指令翻译成 JavaScript

    前言 前些时候研究脚本混淆时,打算先学一些「程序流程」相关的概念.为了不因太枯燥而放弃,决定想一个有趣的案例,可以边探索边学. 于是想了一个话题:尝试将机器指令 1:1 翻译 成 JavaScript ...

  5. 《Django By Example》第三章 中文 翻译 (个人学习,渣翻)

    书籍出处:https://www.packtpub.com/web-development/django-example 原作者:Antonio Melé (译者注:第三章滚烫出炉,大家请不要吐槽文中 ...

  6. 《Django By Example》第二章 中文 翻译 (个人学习,渣翻)

    书籍出处:https://www.packtpub.com/web-development/django-example 原作者:Antonio Melé (译者注:翻译完第一章后,发现翻译第二章的速 ...

  7. 《Django By Example》第一章 中文 翻译 (个人学习,渣翻)

    书籍出处:https://www.packtpub.com/web-development/django-example 原作者:Antonio Melé (译者注:本人目前在杭州某家互联网公司工作, ...

  8. 【翻译】Awesome R资源大全中文版来了,全球最火的R工具包一网打尽,超过300+工具,还在等什么?

    0.前言 虽然很早就知道R被微软收购,也很早知道R在统计分析处理方面很强大,开始一直没有行动过...直到 直到12月初在微软技术大会,看到我软的工程师演示R的使用,我就震惊了,然后最近在网上到处了解和 ...

  9. ASP.NET MVC with Entity Framework and CSS一书翻译系列文章之第一章:创建基本的MVC Web站点

    在这一章中,我们将学习如何使用基架快速搭建和运行一个简单的Microsoft ASP.NET MVC Web站点.在我们马上投入学习和编码之前,我们首先了解一些有关ASP.NET MVC和Entity ...

随机推荐

  1. android中监听layout布局

    android开发可以对layout文件夹中的xml文件里的布局进行监听,并处理事件,如:对RelativeLayout,LinearLayout,FrameLayout,GridLayout等布局容 ...

  2. Case When PK PIVOT

    SELECT *FROM ScoreInfogo Name Course Score---------- ---------- -----------Lucy Chinese 74Jim Math 8 ...

  3. Servlet的一些API使用介绍

    final String rootPath = getServletConfig().getServletContext().getRealPath("/");  获取项目运行的根 ...

  4. 【技术贴】解决Mysql启动服务报错1067 进程意外终止

    无法启动MYSQL服务错误1067 进程意外终止. 我使用2013-10-25_appserv-win32-2.6.0.exe安装的MySql,结果服务起不来. 于是细心机智的我发现,在F:\stud ...

  5. malloc(0)的问题

    http://blog.csdn.net/js_xj/article/details/5826042 解答: 首先来解释malloc(0)的问题,这个语法是对的,而且确实也分配了内存,但是内存空间是0 ...

  6. EMMC 简要介绍

    一直想写一篇关于EMMC的文章,但是因为之前弄了很多PPT,所以一直提不起兴趣,索性直接把之前的一个介绍EMMC的PPT贴出来给大家看看,有什么问题可以直接跟帖,我会第一时间进行解答,谢谢

  7. VC下载文件显示进度条

    VC下载文件显示进度条 逗比汪星人2009-09-18上传   by Koma http://blog.csd.net/wangningyu http://download.csdn.net/deta ...

  8. [cocos2d] 显示状态与文字

    前言: 对于显示数值与文字一般有三种方式可以使用: CCLabelTTF .CCLabelBMFont .CCLabelAtlas 详细区别可参考:cocos2d 中添加显示文字的三种方式(CCLab ...

  9. 如何在多线程中调用winform窗体控件

    由于 Windows 窗体控件本质上不是线程安全的.因此如果有两个或多个线程适度操作某一控件的状态(set value),则可能会迫使该控件进入一种不一致的状态.还可能出现其他与线程相关的 bug,包 ...

  10. 【转】eclipse android 设置及修改生成apk的签名文件 -- custom debug keystore

    原文网址:http://hold-on.iteye.com/blog/2064642 android eclipse 设置及修改生成apk的签名文件 1. 问题: 平时在使用eclipse进行andr ...