[翻译] 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 ...
随机推荐
- poj 1681 Painter's Problem
Painter's Problem 题意:给一个n*n(1 <= n <= 15)具有初始颜色(颜色只有yellow&white两种,即01矩阵)的square染色,每次对一个方格 ...
- 配置mybatis错误总结
### The error may exist in SQL Mapper Configuration ### Cause: org.apache.ibatis.builder.BuilderExce ...
- C# aspnetpager分页
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs& ...
- Android WebView缓存分析
http://blog.csdn.net/a345017062/article/details/8703221 WebView的缓存可以分为页面缓存和数据缓存. 页面缓存是指加载一个网页时的htm ...
- python 重载 __hash__ __eq__
__author__ = 'root' from urlparse import urlparse class host_news(): def __init__(self, id, url): se ...
- CAS单点登录配置[4]:客户端配置
本节介绍一下客户端如何配置,客户端配置没有服务端那么复杂... 客户端Tomcat配置 1 首先确认证书文件已经拷贝到Tomcat的目录下,我们新建两个客户端的web应用,分别命名为Client1,C ...
- 最好的程序员都是行动派(成功者不是那些明知赚钱之法还要推三阻四的人。成功者知道轻重缓急,善于把握今天) good
我相信,所有程序员都需要在下面两点之间找到一个良好的平衡: 1.把自己关在一间私密的办公室里,针对你的程序与编译器展开一次亲密对话. 2.出入公众场合,与其他人公开谈论你的程序. 关于这个话题,我已经 ...
- 解决crystal report水晶报表在浏览器提示bobj未定义的错误
网上的中文文章(比如这篇文章)都是写的部署到服务器后出现的问题,同时也指出要把crystal report的aspnet_client文件夹拷贝到对应项目的根目录里,这样就可以正常显示了,但是具体到我 ...
- JBPM4.4部署到tomcat6异常解决办法
java.lang.LinkageError: loader constraint violation: when resolving interface method "javax.ser ...
- FileZilla 425 Can't open data connection
FileZilla 425 Can't open data connection WIN 2008 SERVER+FileZilla FTP Server,FTP端口:2013 防火墙中已允许FTP ...