0%

NSObject+ModelExtension.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#import <Foundation/Foundation.h>

@protocol NSObjectDelegate <NSObject>

@optional
+ (NSDictionary *)arrayContainModelClass; ///< array 转字典
@optional
+ (NSDictionary *)modelContainModelClass; /// < array 转model

@end


@interface NSObject (ModelExtension)

+ (instancetype)modelWithDict:(id)dict;

@end

NSObject+ModelExtension.m

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#import "NSObject+ModelExtension.h"
#import <objc/runtime.h>

@implementation NSObject (ModelExtension)

+ (instancetype)modelWithDict:(id)dict {
id obj = [[self alloc] init];

unsigned int count;
Ivar *ivarList = class_copyIvarList(self, &count);
for (int i = 0; i < count; i ++) {
Ivar ivar = ivarList[i];

// 获取成员属性
NSString *name = [NSString stringWithUTF8String:ivar_getName(ivar)];

// 处理成员属性名 -> 字典中的key 去掉下划线
NSString *key = [name substringFromIndex:1];

// 根据成员属性名去字典查找对应的value
id value = dict[key];

// 二级转换: 如果字典中还有字典,也需要把对应的字典转换成模型
if ([value isKindOfClass:[NSDictionary class]]) {

// 获取成员变量的类型
NSString *type = [NSString stringWithUTF8String:ivar_getTypeEncoding(ivar)];

// 裁剪类型字符串
NSRange range = [type rangeOfString:@"\""];

type = [type substringFromIndex:range.location + range.length];

range = [type rangeOfString:@"\""];

type = [type substringFromIndex:range.location];

// 根据字符串类型生成类对象
Class moduleClass = NSClassFromString(type);

if (moduleClass) {
value = [moduleClass modelWithDict:value];
}
}

// 三级转换: NSArray 中也是字典 把数组中的字典转化成模型
if ([value isKindOfClass:[NSArray class]]) {
// 判断对应类有没有实现字典数组转模型数组的协议
if ([self respondsToSelector:@selector(arrayContainModelClass)]) {
// 转换成id类型 就能调用任何对象的方法
id idSelf = self;

// 获取数组中字典对应的模型
NSString *type = [idSelf arrayContainModelClass][key];
//生成模型
Class classModel = NSClassFromString(type);
NSMutableArray *arrayM = [NSMutableArray array];
// 遍历字典数组 生成模型数组
for (NSDictionary *dict in value) {
// 字典模型
id model = [classModel modelWithDict:dict];
[arrayM addObject:model];
}
// 把模型数组赋值给value
value = arrayM;
}

// 模型里面套模型
if ([self respondsToSelector:@selector(modelContainModelClass)]) {
id idSelf = self;
NSString *type = [idSelf modelContainModelClass][key];
Class classModel = NSClassFromString(type);
NSMutableArray *arrayM = [NSMutableArray array];
for (NSDictionary *dict in value) {
id model = [classModel modelWithDict:dict];
[arrayM addObject:model];
}
value = arrayM;
}
}
if (value) {// 有值,才需要给模型的属性赋值
// 利用KVC给模型的属性赋值
[obj setValue:value forKey:key];
}
}
return obj;
}

@end

使用 以请求豆瓣电影250为例

DouBanModel.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#import <Foundation/Foundation.h>
#import "NSObject+ModelExtension.h"

@interface MovieInfo : NSObject <NSCoding>

@property (nonatomic, strong) NSDictionary *rating;
@property (nonatomic, assign) NSInteger id;
@property (nonatomic, copy) NSString *original_title;
@property (nonatomic, assign) NSInteger collect_count;
@property (nonatomic, copy) NSArray *directors;
@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *year;
@property (nonatomic, copy) NSArray *casts;
@property (nonatomic, copy) NSArray *genres;
@property (nonatomic, strong) NSDictionary *images;
@property (nonatomic, copy) NSString *subtype;
@property (nonatomic, copy) NSString *alt;

@end


@interface DouBanModel : NSObject <NSObjectDelegate, NSCoding>

@property (nonatomic, assign) NSInteger count;
@property (nonatomic, assign) NSInteger start;
@property (nonatomic, assign) NSInteger total;
@property (nonatomic, copy) NSString *title;
@property (nonatomic, strong) NSArray<MovieInfo *> *subjects;

@end

DouBanModel.m

1
2
3
4
5
@implementation DouBanModel
+ (NSDictionary *)modelContainModelClass {
return @{@"subjects": NSStringFromClass([MovieInfo class])};
}
@end

ViewController.m 网络请求

1
2
3
4
5
6
7
8
9
10
 [[NetWorkTooth shendeInstence] requestWithUrl:@"https://api.douban.com/v2/movie/top250?start=0&count=50" success:^(NSDictionary *dictData) {
DouBanModel *model = [DouBanModel modelWithDict:dictData];
[self saveData:model];
NSLog(@"请求的到的:");
for (MovieInfo *info in model.subjects) {
NSLog(@"%@ - %@", info.title, info.year);
}
} failed:^(NSError *error) {

}];

Demo 地址

https://github.com/GhostClock/Runtime-Demo

面试题:你用过 performSelector 吗?

其实就是动态添加方法:

1
2
RuntimeTest *test = [[RuntimeTest alloc] initWithLog:@"123"];
[test performSelector:@selector(printLog1:log2:) withObject:@"One" withObject:@"Two"]; // 动态添加方法
1
2
3
4
5
6
7
8
9
10
+ (BOOL)resolveInstanceMethod:(SEL)sel {
if (sel == @selector(printLog1:log2:)) {
class_addMethod(self, @selector(printLog1:log2:), (IMP)printLog, "v@:@@");
}
return [super resolveInstanceMethod:sel];
}

void printLog(id self, SEL _cmd, NSString *log1, NSString *log2) {
//TODO
}

Demo 地址

https://github.com/GhostClock/Runtime-Demo

获取一个类的属性列表

1
2
3
4
5
6
7
8
9
- (void)getPropertyList {
unsigned int count;
objc_property_t *propertys = class_copyPropertyList([self class], &count);
for (int i = 0; i < count; i ++) {
objc_property_t property = propertys[i];
NSLog(@"%s %s\n", property_getName(property), property_getAttributes(property));
}
free(propertys);
}

获取一个类的方法列表

1
2
3
4
5
6
7
8
9
- (void)getMethodList {
unsigned int count;
Method *methods = class_copyMethodList([self class], &count);
for (int i = 0; i < count; i ++) {
Method method = methods[i];
NSLog(@"%@ %u \n", NSStringFromSelector(method_getName(method)), method_getNumberOfArguments(method));
}
free(methods);
}

获取成员变量

1
2
3
4
5
6
7
8
9
- (void)getIvarList {
unsigned int count;
Ivar *ivars = class_copyIvarList([self class], &count);
for (int i = 0; i < count; i ++) {
Ivar ivar = ivars[i];
NSLog(@"%s %s \n", ivar_getName(ivar), ivar_getTypeEncoding(ivar));
}
free(ivars);
}

获取协议名

1
2
3
4
5
6
7
8
- (void)getProtocolList {
unsigned int count;
__unsafe_unretained Protocol **protocols = class_copyProtocolList([self class], &count);
for (int i = 0; i < count; i ++) {
NSLog(@"%s", protocol_getName(protocols[i]));
}
free(protocols);
}

Demo 地址

https://github.com/GhostClock/Runtime-Demo

foundation 与 coreFoundation的桥接

1. 用__bridge桥接 NSArray -> CFArrayRef

1
2
3
4
NSArray *fArray = @[@1, @2, @3, @4, @5];
// __bridge本身的意思是 ARC仍然具备这个OC对象的所有权, ARC任然会帮助我们释放
CFArrayRef cfArray = (__bridge CFArrayRef)fArray;
NSLog(@"Size of aray = %li", CFArrayGetCount(cfArray));

2. 用__bridge_retained 桥接 NSArray -> CFArrayRef

1
2
3
4
// __bridge_retained 意味着ARC交出对象的所有权
cfArray = (__bridge_retained CFArrayRef)fArray;
NSLog(@"Size of aray = %li", CFArrayGetCount(cfArray));
CFRelease(cfArray); //手动释放内存

3. 用__bridge_transfer 桥接 CFArrayRef -> NSArray

1
2
// 使ARC获得对象的所有权
fArray = (__bridge_transfer NSArray*)cfArray;

例子

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
const void* RetainCallback(CFAllocatorRef allocator, const void* value) {
return CFRetain(value);
}

void ReleaseCallback(CFAllocatorRef allocator, const void* value) {
CFRelease(value);
}

CFDictionaryKeyCallBacks keyCallbacks = {
0,
RetainCallback,
ReleaseCallback,
NULL, //copyDescription取值为NULL,采用默认实现
//equal和hash发布采用CFEqual和CFHash这二者采用的做法与NSMutableDictionary的默认实现相同
CFEqual, // CFEqual最终会调用isEqual方法
CFHash,
};

CFDictionaryValueCallBacks valueCallback = {
0,
RetainCallback,
ReleaseCallback,
NULL,
CFEqual,
};
// 用CFMutableDictionaryRef来创建NSMutableDictionary
- (void)createMutableDictionaryForCFMutableDictionaryRef {
CFMutableDictionaryRef aCFDicionary = CFDictionaryCreateMutable(NULL, 0, &keyCallbacks, &valueCallback);
NSMutableDictionary *anNSDictionary = (__bridge_transfer NSMutableDictionary *)aCFDicionary;
anNSDictionary[@"a"] = @123;
[anNSDictionary removeObjectForKey:@"a"];
}

Swift 创建栈(Stack)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/// 栈
// 用数组作为栈的线性表的实现
public struct Stack<T> {
// 数组
fileprivate var stackArray = [T]()

// count
public var count: Int {
return stackArray.count
}

// 是否为空
public var isEmpty: Bool {
return stackArray.isEmpty
}

// 顶部元素
public var top: T? {
guard !isEmpty else {
return nil
}
return stackArray.last
}

// push 操作
public mutating func push(_ element: T) {
stackArray.append(element)
}

// pop 操作
public mutating func pop() -> T? {
guard !isEmpty else {
print("stack is empty")
return nil
}
return stackArray.removeLast()
}

// 打印所有元素
public mutating func printAllElement() {
guard count > 0 else {
print("stack is empty")
return
}
print("\npint all stack elements")
for (index, value) in stackArray.enumerated() {
print("[\(index)]\(value)")
}
}
}

使用栈

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
var stack = Stack.init(stackArray: [])
stack.printAllElement()
stack.isEmpty

stack.push(22)
stack.printAllElement()

stack.push(33)
stack.printAllElement()

stack.top

stack.pop()
stack.printAllElement()

stack.pop()
stack.printAllElement()

stack.isEmpty