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. hdu10007

    题意,很简单,给n个点的坐标,求距离最近的一对点之间距离的一半. 第一行是一个数n表示有n个点,接下来n行是n个点的x坐标和y坐标.实数. 这个题目其实就是求最近点对的距离   #include< ...

  2. 如何让U盘支持大于4G的文件

    U盘通常是FAT(*)格式,不能支持大于4G的文件.为了实现这个目的,通常可以把U盘格式化成NTFS或者exFAT,这两种文件系统都支持大于4G的文件. 一.格式化成NTFS第一步首先我们把优盘插入电 ...

  3. C#读取Excel几种方法的体会

    (1) OleDb: 用这种方法读取Excel速度还是非常的快的,但这种方式读取数据的时候不太灵活,不过可以在 DataTable 中对数据进行一些删减修改 这种方式将Excel作为一个数据源,直接用 ...

  4. nexus3添加第三方jar

    最近在看maven的打包及管理,然後就看到nexus,自己在安裝的時候就下載了最新版的nexus-3.2.0-01-win64,按照文档部署后启动,浏览.之前一致使用的是2.0的,所以还是需要导出点点 ...

  5. Accessing Scoped Variables

    To permit the JSP page to access the data, the servlet needs to use setAttribute to store the data i ...

  6. Android使用Dom解析xml

    一.理论准备                 二.上代码  <?xml version="1.0" encoding="utf-8" ?> < ...

  7. CT 来值班,让您安心过新年!

    春节,盼了整整一年的节日,我们一定要抛开工作,狠狠的开心,狠狠的幸福,但是作为苦逼的运维,你们真的能完全抛开工作(对网站不闻不问)吗?OneAPM CT 24 小时监控您的网站,让您无忧无虑过新年. ...

  8. 算法练习之:Doubles

    Doubles Time Limit: 1000MS Memory Limit: 65536KB Problem Description As part of an arithmetic compet ...

  9. 【BZOJ 3473】 字符串 (后缀数组+RMQ+二分 | 广义SAM)

    3473: 字符串 Description 给定n个字符串,询问每个字符串有多少子串(不包括空串)是所有n个字符串中至少k个字符串的子串? Input 第一行两个整数n,k. 接下来n行每行一个字符串 ...

  10. USB OTG

    OTG检测的原理是:USB OTG标准在完全兼容USB2.0标准的基础上,增添了电源管理(节省功耗)功能,它允许设备既可作为主机,也可作为外设操作(两用OTG).USB OTG技术可实现没有主机时设备 ...