您好,欢迎来到伴沃教育。
搜索
您的当前位置:首页description方法和descriptionWithLoc

description方法和descriptionWithLoc

来源:伴沃教育

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

Copyright © 2019- bangwoyixia.com 版权所有 湘ICP备2023022004号-2

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务