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

  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.

目录
相关文章
|
负载均衡 算法 搜索推荐
LBS和LVS的区别
当提到LBS和LVS时,通常指的是以下两个概念: 1. LBS(Location-Based Services):基于位置的服务,是一种利用移动设备的位置信息提供个性化服务的技术。LBS可以通过GPS、Wi-Fi、蜂窝网络等方式获取设备的位置信息,并根据位置信息提供相应的服务,如地图导航、附近商家搜索、位置分享等。LBS在移动互联网时代得到了广泛的应用。 2. LVS(Linux Virtual Server):Linux虚拟服务器,是一种基于Linux操作系统的负载均衡软件。LVS通过将请求分发到多个服务器来平衡负载,提高系统的可用性和性能。LVS可以使用多种负载均衡算法,如轮询、加权轮
623 2
|
数据采集 编解码 运维
PMU
PMU
713 1
|
Web App开发 IDE 前端开发
10款最好用的IDEA插件让我们在2022年生活更美好
我知道你在寻找最好的 Intellij 插件。 瞧,你已经来到了最好的地方了。 在 JAVA 开发人员中广泛使用 Intellij 代码开发工具,无论是 Android 还是原生 JAVA。 有这么多 IDE 进入竞争,但拥有如此庞大的插件市场使 Intellij IDEA 成为最牛逼的赢家。 因此,在这里您将了解到一些精选的最佳 Intellij IDEA 插件。
10款最好用的IDEA插件让我们在2022年生活更美好
|
前端开发
React页面跳转取消上一个页面的所有请求
React页面跳转时取消上一个页面的所有axios请求,通过axios拦截器设置cancelToken,并在页面跳转时调用cancel函数取消未完成的请求。
259 2
|
10月前
|
存储 供应链 安全
探索区块链技术在供应链管理中的应用
本文深入探讨了区块链技术在供应链管理中的应用,分析了其如何提高透明度、安全性和效率。通过具体案例研究,展示了区块链如何解决传统供应链中的信任问题,降低成本,并促进更高效的物流管理。文章还讨论了实施区块链技术面临的挑战和未来发展趋势。
|
11月前
|
Web App开发 Java iOS开发
webp详解
WebP是一种由谷歌开发的图像文件格式,旨在提供更高效的图像压缩方法,以加快网页加载速度。它支持有损和无损压缩模式,并且在相同的视觉质量下,相比JPEG和PNG等格式,文件大小更小,从而优化了网络传输效率。此外,WebP还支持透明度和动画图像。
|
存储 自然语言处理 IDE
Bazel 与 Gradle 构建工具差异对比
Bazel和Gradle强调构建体验的不同方面。在某种程度上,它们的侧重点是互斥的——Gradle对灵活性和非突出性的要求对它的构建结构进行了限制,而Bazel对可靠性和性能的要求产生了强制不可协商的限制。
412 0
|
存储 前端开发 关系型数据库
基于PHP和MySQL数据库实现的学生成绩管理系统
基于PHP和MySQL数据库实现的学生成绩管理系统
1093 0
基于PHP和MySQL数据库实现的学生成绩管理系统