cdts/xdts-ios 3/Pods/DDSqliteManager/SqliteSpec/Classes/DDNSObject+Ext.m

117 lines
3.7 KiB
Mathematica
Raw Normal View History

2023-07-27 09:20:00 +08:00
//
// DDNSObject+Ext.m
// XiYuWang
//
// Created by on 16/5/19.
// Copyright © 2016 Ehsy_Sanli. All rights reserved.
//
#import "DDNSObject+Ext.h"
@implementation NSObject (Ext)
+ (instancetype)objectInitWithDictionary:(NSDictionary *)data {
return [[self alloc] initWithDictionary:data];
}
- (instancetype)initWithDictionary:(NSDictionary *)data {
{
self = [self init];
if (self) {
[self assginToPropertyWithDictionary:data];
}
return self;
}
}
#pragma mark -- Setter
- (SEL) creatSetterWithPropertyName: (NSString *) propertyName{
//1.
if (propertyName.length > 1) {
NSString *tempFirstStr = [propertyName substringToIndex:1];
NSString *tempSecondStr = [propertyName substringFromIndex:1];
tempFirstStr = [tempFirstStr capitalizedString];
propertyName = [tempFirstStr stringByAppendingString:tempSecondStr];
}else {
propertyName = [propertyName capitalizedString];
}
//2.set
propertyName = [NSString stringWithFormat:@"set%@:", propertyName];
//3.set
return NSSelectorFromString(propertyName);
}
/************************************************************************
*
*
*keyValue
*
************************************************************************/
- (void)assginToPropertyWithDictionary: (NSDictionary *) data {
if (data == nil) {
return;
}
//1.key
NSArray *dicKey = [data allKeys];
//2.key, setterValuesetter
//
for (int i = 0; i < dicKey.count; i ++) {
///2.1 getSetterSelWithAttibuteName set
SEL setSel = [self creatSetterWithPropertyName:dicKey[i]];
if ([self respondsToSelector:setSel]) {
//2.2 keyvalue
NSObject *value = [NSString stringWithFormat:@"%@", data[dicKey[i]]];
//2.3 setter
[self performSelectorOnMainThread:setSel
withObject:value
waitUntilDone:[NSThread isMainThread]];
}
}
}
/**************************************/
- (id)objConvertToStr {
if (self == nil) {
return @"";
}else if ([self isKindOfClass:[NSString class]]) {
return (NSString *)self;
}else if ([self isKindOfClass:[NSNumber class]]) {
return [((NSNumber *)self) stringValue];
}
return [[NSString alloc] initWithData:[NSJSONSerialization dataWithJSONObject:self options:NSJSONWritingPrettyPrinted error:nil] encoding:NSUTF8StringEncoding];
}
- (id)jsonStrConvertToObj {
if (![self isKindOfClass:[NSString class]]) {
return [NSNull null];
}
NSString *json = (NSString *)self;
return [NSJSONSerialization JSONObjectWithData:[json dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingMutableContainers error:nil];
}
+ (NSArray *)xxd_objArrFromKeyValues:(id)res {
return [self mj_objectArrayWithKeyValuesArray:res];
}
+ (instancetype)xxd_objFromKeyValue:(id)res {
return [self mj_objectWithKeyValues:res];
}
- (NSDictionary *)xxd_keyValues {
return self.mj_keyValues;
}
+ (NSArray *)xxd_keyValuesArrFromObjArr:(NSArray *)res {
return [self mj_keyValuesArrayWithObjectArray:res];
}
@end