只需建立一個.m文件谊娇,添加如下代碼迹鹅,導入工程中就可以了
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
@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 *str = [NSMutableString string];
[str appendString:@"{\n"];
// 遍歷字典的所有鍵值對
[self enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
[str appendFormat:@"\t%@ = %@,\n", key, obj];
}];
[str appendString:@"}"];
// 查出最后一個,的范圍
NSRange range = [str rangeOfString:@"," options:NSBackwardsSearch];
if (range.length) {
// 刪掉最后一個,
[str deleteCharactersInRange:range];
}
return str;
}
@end
@implementation NSArray (Log)
- (NSString *)descriptionWithLocale:(id)locale
{
NSMutableString *str = [NSMutableString string];
[str appendString:@"[\n"];
// 遍歷數(shù)組的所有元素
[self enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
[str appendFormat:@"%@,\n", obj];
}];
[str appendString:@"]"];
// 查出最后一個,的范圍
NSRange range = [str rangeOfString:@"," options:NSBackwardsSearch];
if (range.length) {
// 刪掉最后一個,
[str deleteCharactersInRange:range];
}
return str;
}
@end