一,对于NSArray和NSDictionary用descriptionWithLocale:方法
@interfaceNSArray (Log)
@end
@interfaceNSDictionary (Log)
@end
#import"NSArray+Log.h"
@implementationNSArray (Log)
- (NSString *)descriptionWithLocale:(id)locale
{
NSMutableString *strM = [NSMutableString stringWithString:@"(\n"];
[self enumerateObjectsUsingBlock:^(idobj, NSUInteger idx,BOOL*stop)
{
[strM appendFormat:@"\t%@,\n", obj];
}];
[strM appendString:@")"];
return strM;
}
@end
@implementationNSDictionary (Log)
- (NSString *)descriptionWithLocale:(id)locale
{
NSMutableString *strM = [NSMutableString stringWithString:@"{\n"];
[self enumerateKeysAndObjectsUsingBlock:^(idkey,idobj,BOOL*stop) {
[strM appendFormat:@"\t%@ = %@;\n", key, obj];
}];
[strM appendString:@"}\n"];
return strM;
}
@end
二,对于一般的对象用description方法
@interfaceHMPerson :NSObject
@property(nonatomic,copy)NSString* name;
@property(nonatomic,assign)int age;
@end
#import"HMPerson.h"
@implementationHMPerson
- (NSString*)description
{
return [NSString stringWithFormat:@"%@{name:%@,age:%d}",[super description],self.name,self.age];
}
@end