當(dāng)后臺給的參數(shù)過多時??梢允褂眠@個分類
傳入一個字典,給你一個model屬性的代碼(可根據(jù)自己的參數(shù)類型做更改)。
.h文件
@interface NSObject (Property)
+ (void)createPropertyCodeWithDict:(NSDictionary *)dict;
@end
.m文件
@implementation NSObject (Property)
+ (void)createPropertyCodeWithDict:(NSDictionary *)dict
{
NSMutableString *strM = [NSMutableString string];
// 遍歷字典
[dict enumerateKeysAndObjectsUsingBlock:^(id _Nonnull propertyName, id _Nonnull value, BOOL * _Nonnull stop) {
// NSLog(@"%@ %@",propertyName,[value class]);
NSString *code;
NSLog(@"%@",[value class]);
if ([value isKindOfClass:NSClassFromString(@"__NSCFString")]) {
code = [NSString stringWithFormat:@"@property (nonatomic, copy) NSString *%@;",propertyName]
;
}else if ([value isKindOfClass:NSClassFromString(@"__NSCFNumber")]){
code = [NSString stringWithFormat:@"@property (nonatomic, assign) NSInteger %@;",propertyName]
;
}else if ([value isKindOfClass:NSClassFromString(@"__NSCFArray")]){
code = [NSString stringWithFormat:@"@property (nonatomic, copy) NSArray *%@;",propertyName]
;
}else if ([value isKindOfClass:NSClassFromString(@"__NSCFDictionary")]){
code = [NSString stringWithFormat:@"@property (nonatomic, copy) NSDictionary *%@;",propertyName]
;
}else if ([value isKindOfClass:NSClassFromString(@"__NSCFBoolean")]){
code = [NSString stringWithFormat:@"@property (nonatomic, assign) BOOL %@;",propertyName]
;
}
else if ([value isKindOfClass:NSClassFromString(@"__NSArray0")]){
code = [NSString stringWithFormat:@"@property (nonatomic, copy) NSArray *%@;",propertyName]
;
}
else if ([value isKindOfClass:NSClassFromString(@"__NSArrayI")]){
code = [NSString stringWithFormat:@"@property (nonatomic, copy) NSArray *%@;",propertyName]
;
}
else if ([value isKindOfClass:NSClassFromString(@"NSTaggedPointerString")]){
code = [NSString stringWithFormat:@"@property (nonatomic, copy) NSString *%@;",propertyName]
;
}
[strM appendFormat:@"\n%@\n",code];
}];
NSLog(@"%@",strM);
}
@end
用法,直接傳入一個字典,會在控制臺打印model屬性,直接復(fù)制粘貼即可
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。