This repository has been archived by the owner on Sep 6, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
121 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// | ||
// NSObject+DebugDescription.h | ||
// DebugDescription | ||
// | ||
// Created by Rakuyo on 2017/10/6. | ||
// Copyright © 2017年 Rakuyo. All rights reserved. | ||
// | ||
// http://www.cocoachina.com/ios/20170728/20055.html | ||
|
||
#import <Foundation/Foundation.h> | ||
|
||
// 打印模型时打印其下元素。 | ||
@interface NSObject (DebugDescription) | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// | ||
// NSObject+DebugDescription.m | ||
// DebugDescription | ||
// | ||
// Created by Rakuyo on 2017/10/6. | ||
// Copyright © 2017年 Rakuyo. All rights reserved. | ||
// | ||
|
||
#import "NSObject+DebugDescription.h" | ||
#import <objc/runtime.h> | ||
|
||
@implementation NSObject (DebugDescription) | ||
|
||
- (NSString *)description { | ||
|
||
if ([self isKindOfClass:[NSArray class]] | ||
|| [self isKindOfClass:[NSDictionary class]] | ||
|| [self isKindOfClass:[NSNumber class]] | ||
|| [self isKindOfClass:[NSString class]]) { | ||
return self.description; | ||
} | ||
|
||
return [self debugDescription]; | ||
} | ||
|
||
- (NSString *)debugDescription { | ||
if ([self isKindOfClass:[NSArray class]] | ||
|| [self isKindOfClass:[NSDictionary class]] | ||
|| [self isKindOfClass:[NSNumber class]] | ||
|| [self isKindOfClass:[NSString class]]) { | ||
return self.debugDescription; | ||
} | ||
|
||
// 初始化一个字典 | ||
NSMutableDictionary *dic = [NSMutableDictionary dictionary]; | ||
|
||
// 得到当前 Class 的所有属性 | ||
uint count; | ||
objc_property_t *properties = class_copyPropertyList([self class], &count); | ||
|
||
// 利用KVC获取属性值 | ||
for (int i = 0; i < count; i++) { | ||
objc_property_t property = properties[i]; | ||
NSString *name = @(property_getName(property)); | ||
|
||
// 设置一个默认值。 | ||
id value = [self valueForKey:name]?:@"nil"; | ||
|
||
[dic setObject:value forKey:name]; | ||
} | ||
|
||
free(properties); | ||
|
||
return [NSString stringWithFormat:@"<%@: %p> -- %@", [self class], self, dic]; | ||
} | ||
|
||
@end |