Giter Club home page Giter Club logo

cruserdefaults's Introduction

CRUserDefaults

[![CI Status](http://img.shields.io/travis/Carlos Rios/CRUserDefaults.svg?style=flat)](https://travis-ci.org/Carlos Rios/CRUserDefaults) Version License Platform

CRUserDefaults is helper class that offers a interface to write data on [NSUserDefaults standardUserDefaults]. The distinctive value CRUserDefaults has, is allows that standardUserDefaults acts as an object, so you only have to add properties to it, then declare this property as @dynamic and automatically you have a new property in standardUserDefaults.

Also you can declare a property of any object type and it will be store too, but you should implement NSCoding protocol for custom objects.

The only concern is all property have to be Object pointers, so if you are going to declare boolean property you must declare it as NSNumber.

Why use CRUserDefaults

  1. Because with CRUserDefaults you can keep encapsulated the data store in NSUserDefault in only one place.
  2. Because offers better documentation to other developers what are the Global Variables of your app.
  3. Because it acts as interface to store you custom data, and you don't worry about how it store information and how load it.
  4. Because you can use this class as caching mechanism,

Usage

Simple, implements a subclass of this and add properties to it. Remember declare all properties as @dynamic in the implementation file.

Subclasing

@interface CRCustomDefaults : CRUserDefaults

@property (nonatomic, strong) USer * current;

+ (CRCustomDefaults *) shared;

@end

@implementation CRUserDefaults
@dynamic current;

+ (CRCustomDefaults *) shared
{
     return [super shared];
}
@end

User class implementing NSCoding Protocol

If you want store a custom object in CRUserDefaults, you must implement NSCoding protocol on it. Look at next example of a CRUser class.

@interface CRUser : NSObject <NSCoding>

@property (nonatomic, copy) NSString name;
@property (nonatomic, copy) NSString lastName;
@property (nonatomic, copy) NSString email;
@property (nonatomic, copy) NSNumber age;

- (instancetype)initWithName:(NSString *)name lastName:(NSString *)lastName;

@end

@implementation CRUser

- (instancetype)initWithName:(NSString *)name lastName:(NSString *)lastName
{
 self = [super init];
 if (self) {
     _name = [name copy];
     _lastName = [lastName copy];
 }
  return self;
}

-(id) initWithCoder:(NSCoder *) aDecoder
{
     _name = [aDecoder decodeObjectForKey:knameKey];
     _lastName = [aDecoder decodeObjectForKey:klastNameKey];
     _email = [aDecoder decodeObjectForKey:kemailKey];
     _age = [aDecoder decodeObjectForKey:kageKey];
     return self;
}

-(void) encodeWithCoder:(NSCoder *) encoder
{
     [encoder encodeObject:_name forKey:knameKey];
     [encoder encodeObject:_lastName forKey:klastNameKey];
     [encoder encodeObject:_email forKey:kemailKey];
     [encoder encodeObject:_age forKey:kageKey];
}

@end

To run the example project, clone the repo, and run pod install from the Example directory first.

Requirements

iOS 5 or higher.

Installation

CRUserDefaults is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod "CRUserDefaults"

Author

Carlos Rios, [email protected]

License

CRUserDefaults is available under the MIT license. See the LICENSE file for more info.

cruserdefaults's People

Contributors

riosc avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

aristidescc

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.