Giter VIP home page Giter VIP logo

gvuserdefaults's Introduction

GVUserDefaults - NSUserDefaults access via properties

Badge w/ Version Badge w/ Platform

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?

Usage

Create a category on GVUserDefaults, add some properties in the .h file and make them @dynamic in the .m file.

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

You can even save defaults by setting the property:

[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":

- (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:

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

Install

Install via CocoaPods (pod 'GVUserDefaults') or drag the code in the GVUserDefaults subfolder to your project.

Issues and questions

Have a bug? Please create an issue on GitHub!

Contributing

GVUserDefaults is an open source project and your contribution is very much appreciated.

  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.

gvuserdefaults's People

Contributors

atomkirk avatar jerryhjones avatar kevinrenskers avatar luosheng avatar romaonthego avatar sibljon avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

gvuserdefaults's Issues

Updating the transformKey

Hi there,

Firstly thanks for the great contribution!

Would there be any way to change the transformKey at a later time? The use case I have in mind is to enable namespacing the persisted key/values by something dynamic like a UUID.

-James

Multi user switching

When I went up to multiple users to switch , I call transformKey method for each attribute and user name prefix, but only in the program to restart when walk this method, so when program alive multiple users to switch is not

Suit -> Suite?

NSUserDefaults has the method initWithSuiteName

But GVUserDefaults has - (NSString *)suitName (notice: suit and not suite).

Might be confusing?

Default value cannot be updated when the app switches the users' accounts

Let me describe my question explicitly. For example, I define a key named 'autoSpeak ' in the GVUserDefault's category,and I set its default value is YES in the selector 'setupDefaults'. Then I run my app for the very first time, and log in. The UISwitch controlled by autoSpeak is close. Then I killed my app ,and then run it again,the UISwitch is open now. The same question happened when I switch to another user account.
I looked through the source code. I found setupDefaults is called in the selector init, but the object of GVUserDefault is singleton, so that is the reason, I thought.
Now I solve this problem by called setupDefaults again. But I do not think this is the perfect plan. I wish you, the author, can fix this issue in the source code .

Observing changes to GVUserDefaults property values

As far as I can tell, there is no support for observing property value changes via NSNotificationCenter or KVO (or any other method like block-callbacks, for that matter). What are your thoughts on the prospect of this feature?

transformKey conflict?

Is it ok to subclass GVUserDefaults?

The reason I asked is because we are planning to use GVUserDefaults in a pod. If an app uses our pod and GVUserDefaults, and both the app and our pod overwrite key prefix with transformKey in the categories, would it create a conflict?

synchronize

-[NSUserDefaults synchronize] is slow on iOS, and unnecessary on Mac OS X, synchronizing unconditionally on set is virtually never the right choice on either platform

Int and bool values not being stored in iOS 7

Int and bool values are not being saved across sessions, even when forcing [[NSUserDefaults standardUserDefaults] synchronize]; after every set. This seems to only be happening on iOS 7.x, 8.x works fine.

bug:sometime store property failure

Hi. needs help.
I find sometime store property failure.
my category GVUserDefaults (YOSProperties) has property like this:

@Property (nonatomic, weak) NSString *currentLoginID;

when network request success. I save userId in success block:
[GVUserDefaults standardUserDefaults].currentLoginID = model.ID;
It work this time. I can get data from [GVUserDefaults standardUserDefaults].currentLoginID
But reopen my app. It doesn't work. [GVUserDefaults standardUserDefaults].currentLoginID return nil

I replace
[GVUserDefaults standardUserDefaults].currentLoginID = model.ID;
to
[GVUserDefaults standardUserDefaults].currentLoginID = model.ID;
[[NSUserDefaults standardUserDefaults] synchronize];

It works all the time.
thinks.

Swift support

Objective c properties of course do not quite work in swift, but can similar idea be accomplished in swift? So that it is easy and convenient to access NSUserDefaults via properties in swift?

would be cool to create a hook method that is invoked when GVUserDefaults singleton is instantiated

I'd like to have a place where I can register userdefaults as indicated in the README. At present I have to somewhere in my code call the [[NSUserDefaults standardUserDefaults] registerDefaults:...];

But I'd like to keep the defautls related code in one file, ideally the same category file that I created for all user defaults...

It would be cool if GVUserDefaults can call a method when it is instantiated the similar way transformKey: method is called (when defined)

thanx alot,
Martin

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.