码迷,mamicode.com
首页 > 其他好文 > 详细

打印NSLog分类 Foundation+Log.m

时间:2015-08-03 11:37:14      阅读:137      评论:0      收藏:0      [点我收藏+]

标签:


#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"];

    

    // 遍历数组的所有元素

    [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


版权声明:本文为博主原创文章,未经博主允许不得转载。

打印NSLog分类 Foundation+Log.m

标签:

原文地址:http://blog.csdn.net/guoyule2010/article/details/47253399

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!