之前可就是扔一个分类在工程里,今天又遇到了,关键项目是Swift的,想想把之前写的分类托进来还得改改太麻烦了,于是是想换另外一种新鲜的做法。
一、复制乱码
二、放入Chrome的网页检查器里的控制台,然后回车,就能查看了
更新:处理更优的做法。一个分类搞定
```objc
#import@implementation UIView(Log)
+ (NSString *)searchAllSubviews:(UIView *)superview
{
NSMutableString *xml = [NSMutableString string];
NSString *class = NSStringFromClass(superview.class);
class = [class stringByReplacingOccurrencesOfString:@"_" withString:@""];
[xml appendFormat:@"<%@ frame=\"%@\">\n", class, NSStringFromCGRect(superview.frame)];
for (UIView *childView in superview.subviews) {
NSString *subviewXml = [self searchAllSubviews:childView];
[xml appendString:subviewXml];
}
[xml appendFormat:@"\n", class];
return xml;
}
- (NSString *)description
{
return [UIView searchAllSubviews:self];
}
@end
@implementation NSDictionary (Log)
- (NSString *)descriptionWithLocale:(id)locale
{
NSMutableString *string = [NSMutableString string];
// 开头有个{
[string appendString:@"{\n"];
// 遍历所有的键值对
[self enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
[string appendFormat:@"\t%@", key];
[string appendString:@" : "];
[string appendFormat:@"%@,\n", obj];
}];
// 结尾有个}
[string appendString:@"}"];
// 查找最后一个逗号
NSRange range = [string rangeOfString:@"," options:NSBackwardsSearch];
if (range.location != NSNotFound)
[string deleteCharactersInRange:range];
return string;
}
@end
@implementation NSArray (Log)
- (NSString *)descriptionWithLocale:(id)locale
{
NSMutableString *string = [NSMutableString string];
// 开头有个[
[string appendString:@"[\n"];
// 遍历所有的元素
[self enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
[string appendFormat:@"\t%@,\n", obj];
}];
// 结尾有个]
[string appendString:@"]"];
// 查找最后一个逗号
NSRange range = [string rangeOfString:@"," options:NSBackwardsSearch];
if (range.location != NSNotFound)
[string deleteCharactersInRange:range];
return string;
}
@end
```