Coder Social home page Coder Social logo

zhahao / ccuserdefaultsmanager Goto Github PK

View Code? Open in Web Editor NEW
5.0 2.0 1.0 32 KB

使对NSUserDefaults的存取操作具有更高的内聚性.💥Make access to the NSUserDefaults operation has a higher cohesiveness.

License: MIT License

Ruby 2.48% Objective-C 97.52%
runtime nsuserdefaults pod ios

ccuserdefaultsmanager's Introduction

CCUserDefaultsManager

CocoaPods安装

  1. 在 Podfile 中添加 pod 'CCUserDefaultsManager'。如果安装失败,请更新本地pod库pod repo update
  2. 执行 pod install 或 pod update。
  3. 导入CCUserDefaultsManager.h

简介

这是一个用来集中式管理NSUserDefaults存储的框架,使对NSUserDefaults的存取操作具有更高的内聚性,框架原理是利用了Objective-C的runtime特性,动态修改了类的property行为。当使用[[CCUserDefaultsManager sharedManager] addClass:XXClass]方法,那么XXClass的所有成员变量的getset都会映射成NSUserDefaults对应的存取方法.

 支持的存储类型:

  • c语言类型,仅支持整形、浮点型、布尔型,包括NSInteger,CGFloat等
  • oc对象类型,仅支持NSString, NSData, NSNumber, NSDate, NSArray, NSDictionary ,NSURL等不可变版本

使用

新建一个类

/// .h文件
@interface CCUserDefault : NSObject

/// 使用单例
@property (class, readonly, strong) CCUserDefault *sharedManager;

/// c语言类型,仅支持整形、浮点型、布尔型,包括NSInteger,CGFloat等
@property (nonatomic, assign) int intType;

/// oc对象类型,仅支持NSString, NSData, NSNumber, NSDate, NSArray, NSDictionary ,NSURL等不可变版本
@property (nonatomic, strong) NSString *string;

/// 忽略的成员变量,需要实现CCUserDefaultsManager协议
@property (nonatomic, strong) NSString *ignoreString;

@end


/// .m文件
#import "CCUserDefaultsManager.h"

@implementation CCUserDefault

+ (CCUserDefault *)sharedManager
{
    static CCUserDefault *_mgr = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _mgr = [CCUserDefault new];
    });
    return _mgr;
}

+ (void)load
{
    /// 将CCUserDefault添加到CCUserDefaultsManager中,那么CCUserDefault的成员变量的`set`和`get`方法都会映射成与`NSUserDefaults`对应的存取方法
    [[CCUserDefaultsManager sharedManager] addClass:self];
}
@end

使用该类

存值

CCUserDefault *defaults = [CCUserDefault sharedManager];
defaults.intType = 1;
defaults.string = @"string";
...

取值

CCUserDefault *defaults = [CCUserDefault sharedManager];
NSLog(@"%d%@",defaults.intType,defaults.string);

CCUserDefault类的所有成员变量存取都会映射到NSUserDefaults

其他功能:

  • 添加类的所有属性前缀
  • 添加黑名单

系统要求

该项目最低支持 iOS 7.0 和 Xcode 8.0

许可证

CCUserDefaultsManager 使用 MIT 许可证,详情见 LICENSE 文件。

ccuserdefaultsmanager's People

Contributors

zhahao avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

mohsinalimat

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.