首页 > 代码库 > 重写NSArray与NSDictionary的descriptionWithLocale方法
重写NSArray与NSDictionary的descriptionWithLocale方法
重写NSArray的descriptionWithLocale方法:
NSArray+log.h
#import <Foundation/Foundation.h>@interface NSArray (log)@end
NSArray+log.m
#import "NSArray+log.h"@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
重写NSDictionary的descriptionWithLocale方法:
NSDictionary.h
#import <Foundation/Foundation.h>@interface NSDictionary (log)@end
NSDictionary.m
#import "NSDictionary+log.h"@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
重写NSArray与NSDictionary的descriptionWithLocale方法
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。