博客> iOS链式语法功能实践(2) 数据对象中的使用
iOS链式语法功能实践(2) 数据对象中的使用
2018-07-22 13:08 评论:0 阅读:263 二日三点
ios 链式语法

本次链式语法尝试的是在数据模型中的使用,首先还是先来观看结构

 DEA39A5C-7745-4525-A263-B5A6C9A3070D.png

看下数据模型是什么样子的.

#import <Foundation>
#import "MKChainedClass.h"

@interface User : MKChainedClass

@property (copy, nonatomic) NSString *userId;

@property (copy, nonatomic) NSString *userName;

@property (copy, nonatomic) NSString *userAge;

@property (copy, nonatomic) NSString *userSex;

@end
#import "User.h"

@implementation User

@end
#import <Foundation>
#import "MKChainedClass.h"

@interface Focus : MKChainedClass

@property (assign, nonatomic) int type;

@property (strong, nonatomic) NSString *imageUrl;

@end

@interface FocusList : MKChainedClass

@property (strong, nonatomic) NSArray<Focus> *list;

@end
#import "Focus.h"

@implementation Focus

@end

@implementation FocusList

@end

这两个模型想必大家都经常见,都是常见的样子.但是他们的父类是继承于我写的NSObject类的,是不是和JSONModel很像

再次,我们来看下 我在我写的那个NSObject类里干了什么事情

#import <Foundation>
#import <objc>
#import "MKChainedPropertyClass.h"

@interface MKChainedClass : MKChainedPropertyClass

#pragma mark - SubClass - Version_0.0.1

/**
 Chain assignment Will be divided into two forms A is a form of array (recommended) in the form of another way to use a dictionary

 链式赋值 共分为两种形式 一种是数组的形式(推荐) 另外一种使用字典的形式*/

/**
 数组形式的链式赋值
 */
@property (copy, nonatomic) MKChainedClass *(^initParamsWithArray)(NSArray *params);

/**
 字典形式的链式赋值
 */
@property (copy, nonatomic) MKChainedClass *(^intParamsWithDictionary)(NSDictionary *params);

/**
 是否允许属性值为空

 @return 是否
 */
- (BOOL)propertyAllowEmpty;
#import "MKChainedClass.h"

@interface MKChainedClass()

@end

@implementation MKChainedClass

#pragma mark - ChainedMethond

- (MKChainedClass *(^)(NSArray*))initParamsWithArray {
    return ^MKChainedClass *(NSArray *params) {
        [self setAllPropertiesWithArray:params];
        return self;
    };
}

- (MKChainedClass *(^)(NSDictionary *))intParamsWithDictionary {
    return ^MKChainedClass *(NSDictionary *params) {
        [self setAllPropertiesWithDictionary:params];
        return self;
    };
}

#pragma mark - setValueMethond

- (void)setAllPropertiesWithArray:(NSArray *)params{

    u_int count;//属性的数量

    //获取属性名称
    objc_property_t *properties  =class_copyPropertyList([self class], &count);

    //是否允许空值的存在
    BOOL propertyAllowEmpty = [self propertyAllowEmpty];

    //当允许空值存在的时候
    if (propertyAllowEmpty == YES) {

        //当传入的值小于属性数量的时候
        if (params.count < count>= count) {

            [self setValueWithCount:params.count WithParams:params withProperties:properties];

        }
    }

    //当不允许空值存在的时候
    if (propertyAllowEmpty == NO) {

        //如果传入的值和属性数量不一致的时候
        if (params.count != count) {

            return NSLog(@"current params count is not equal to properties");

        }else{
            //当传入一致的时候
            [self setValueWithCount:params.count WithParams:params withProperties:properties];
        }
    }

    free(properties);
}

- (void)setAllPropertiesWithDictionary:(NSDictionary *)params {
    u_int count;//属性的数量

    //获取属性名称
    objc_property_t *properties  = class_copyPropertyList([self class], &count);

    //是否允许空值的存在
    BOOL propertyAllowEmpty = [self propertyAllowEmpty];

    //当允许空值存在的时候
    if (propertyAllowEmpty == YES) {

        //当传入的值小于属性数量的时候
        if (params.count < count>= count) {
            [self setValueWithCount:count WithDictionaryParams:params withProperties:properties];
        }
    }

    //当不允许空值存在的时候
    if (propertyAllowEmpty == NO) {

        //如果传入的值和属性数量不一致的时候
        if (params.count != count) {

            return NSLog(@"current params count is not equal to properties");

        }else{
            //当传入一致的时候
            [self setValueWithCount:count WithDictionaryParams:params withProperties:properties];

        }
    }
    free(properties);
}

- (BOOL)propertyAllowEmpty {
    return YES;
}

可以看到,我在这个类中做了链式转换,和对数据的判断.虽然比较粗糙,大家将就看 这里面有用到runtime的知识 获取了当前类的所有的属性,然后一一赋值

这个类中进行了一一赋值的操作

#import <Foundation>
#import <objc>

@interface MKChainedPropertyClass : NSObject

//根据不同的方式来进行数据赋值

- (void)setValueWithCount:(NSUInteger)count WithParams:(NSArray *)params withProperties:(objc_property_t *)properties;

- (void)setValueWithCount:(NSUInteger)count WithDictionaryParams:(NSDictionary *)params withProperties:(objc_property_t *)properties;

@end
#import "MKChainedPropertyClass.h"

@implementation MKChainedPropertyClass

- (void)setValueWithCount:(NSUInteger)count WithParams:(NSArray *)params withProperties:(objc_property_t *)properties{
    for (int i = 0; i &lt; count; i++){

        const char* propertyName =property_getName(properties[i]);

        NSString *keyName = [NSString stringWithUTF8String: propertyName];

        [self setValue:params[i] forKey:keyName];

        NSLog(@"properties : %@",[NSString stringWithUTF8String: propertyName]);

    }
}

- (void)setValueWithCount:(NSUInteger)count WithDictionaryParams:(NSDictionary *)params withProperties:(objc_property_t *)properties {
    for (int i = 0; i &lt; count; i++){

        const char* propertyName = property_getName(properties[i]);

        NSString *keyName = [NSString stringWithUTF8String:propertyName];

        [self setValue:params[keyName] ? params[keyName] : @"null" forKey:keyName];

        NSLog(@"properties : %@",[NSString stringWithUTF8String: propertyName]);

    }
}

下面看下具体处理时候的表现

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    //1.链式写法
    User *user = (User *)[[User alloc] init].initParamsWithArray(@[@"10086",@"张三",@"18",@"女"]);

    NSLog(@"user.username : %@ user.userid : %@",user.userName,user.userId);

    Focus *focus = (Focus *)[[Focus alloc] init].initParamsWithArray(@[@1,@"123123123"]);

    FocusList *list = (FocusList *)[[FocusList alloc] init].initParamsWithArray(@[@[focus]]);

    NSLog(@"FocusList.focus.imageUrl : %@",[list.list[0] valueForKeyPath:@"imageUrl"]);

    //2.oc段落式写法
    User *ocUser = [[User alloc] init];
    [ocUser setUserId:@"10086"];
    [ocUser setUserName:@"张三"];
    [ocUser setUserAge:@"18"];
    [ocUser setUserSex:@"女"];

    NSLog(@"user.username : %@ user.userid : %@",ocUser.userName,ocUser.userId);

    Focus *ocFocus = [[Focus alloc] init];
    [ocFocus setType:1];
    [ocFocus setImageUrl:@"123434"];

    FocusList *ocList = [[FocusList alloc] init];
    [ocList setList:@[ocFocus]];
}

what fuk? 好像并没有什么直观的体现!当然如果你的模型层比较好层次分明的那么感觉这个比较鸡肋一点,如果你的模型层 N多个属性 那么你可以考虑用下这个方法来帮助你更好的加快码字的速度。本人感觉速度有提高,但是理解力没有OC段落式好,当然老鸟除!

下一篇 自己写的一个链式语法开源库 MKChained 地址:http://blog.cocoachina.com/article/70525

收藏
0
sina weixin mail 回到顶部