description方法和descriptionWithLoc

2017-03-10  本文已影响150人  流沙3333

一,对于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

上一篇 下一篇

猜你喜欢

热点阅读