unsigned int outCount = 0; //得到Class c全部屬性這裏的c是[Model class] objc_property_t *properties = class_copyPropertyList(c, &outCount); for (int i = 0; i < outCount; i++) { objc_property_t propert = properties[i]; //得到屬性名 NSString *key = @(property_getName(propert)); //得到屬性類型,如CGFloat、nonatomic、copy等信息 NSString *type = @(property_getAttributes(propert)); NSLog(@"key = %@ , type = %@", key, type); }
Model模型以下json
//屬性} typedef void(^block)(); @interface Model : NSObject @property (nonatomic, copy) NSString *q_NSString; @property (nonatomic, assign) CGFloat q_CGFloat; @property (nonatomic, assign) CGRect q_CGRect; @property (nonatomic, assign) double q_double; @property (nonatomic, assign) int q_int; @property (nonatomic, assign) BOOL q_bool; @property (nonatomic, assign) float q_float; @property (nonatomic, assign) short q_short; @property (nonatomic, assign) long q_long; @property (nonatomic, assign) long long q_longlong; @property (nonatomic, assign) Point q_point; @property (nonatomic, strong) id q_id; @property (nonatomic, weak) id<NSObject> q_delegate; @property (nonatomic, copy) block q_block; @property (nonatomic, strong) Model1 *q_model1; @property SEL q_SEL; @property Class q_Class; @property Ivar q_Ivar; @property Method q_Method;
輸出結果爲swift
key = q_NSString , type = T@"NSString",C,N,V_q_NSString key = q_CGFloat , type = Td,N,V_q_CGFloat key = q_CGRect , type = T{CGRect={CGPoint=dd}{CGSize=dd}},N,V_q_CGRect key = q_double , type = Td,N,V_q_double key = q_int , type = Ti,N,V_q_int key = q_bool , type = TB,N,V_q_bool key = q_float , type = Tf,N,V_q_float key = q_short , type = Ts,N,V_q_short key = q_long , type = Tq,N,V_q_long key = q_longlong , type = Tq,N,V_q_longlong key = q_point , type = T{Point=ss},N,V_q_point key = q_id , type = T@,&,N,V_q_id key = q_delegate , type = T@"<NSObject>",W,N,V_q_delegate key = q_block , type = T@?,C,N,V_q_block key = q_model1 , type = T@"Model1",&,N,V_q_model1 key = q_SEL , type = T:,V_q_SEL key = q_Class , type = T#,&,V_q_Class key = q_Ivar , type = T^{objc_ivar=},V_q_Ivar key = q_Method , type = T^{objc_method=},V_q_Method
NSString *const MJPropertyTypeInt = @"i"; NSString *const MJPropertyTypeShort = @"s"; NSString *const MJPropertyTypeFloat = @"f"; NSString *const MJPropertyTypeDouble = @"d"; NSString *const MJPropertyTypeLong = @"l"; NSString *const MJPropertyTypeLongLong = @"q"; NSString *const MJPropertyTypeChar = @"c"; NSString *const MJPropertyTypeBOOL1 = @"c"; NSString *const MJPropertyTypeBOOL2 = @"b"; NSString *const MJPropertyTypePointer = @"*"; NSString *const MJPropertyTypeIvar = @"^{objc_ivar=}"; NSString *const MJPropertyTypeMethod = @"^{objc_method=}"; NSString *const MJPropertyTypeBlock = @"@?"; NSString *const MJPropertyTypeClass = @"#"; NSString *const MJPropertyTypeSEL = @":"; NSString *const MJPropertyTypeId = @"@";
if ([value isKindOfClass:[NSStringclass]]) { if (propertyClass == [NSURL class]) { // NSString -> NSURL // 字符串轉碼 value = [value mj_url]; } else if (type.isNumberType) { NSString *oldValue = value; // NSString -> NSNumber value = [numberFormatter_ numberFromString:oldValue]; // 若是是BOOL if (type.isBoolType) { // 字符串轉BOOL(字符串沒有charValue方法) // 系統會調用字符串的charValue轉爲BOOL類型 NSString *lower = [oldValue lowercaseString]; if ([lower isEqualToString:@"yes"] || [lower isEqualToString:@"true"]) { value = @YES; } else if ([lower isEqualToString:@"no"] || [lower isEqualToString:@"false"]) { value = @NO; } } } }
- (id)initWithCoder:(NSCoder *)decoder - (void)encodeWithCoder:(NSCoder *)encoder
+ (NSDictionary *)dictionaryWithJsonString:(NSString *)jsonString { if (jsonString == nil) { return nil; } NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding]; NSError *error; NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&error]; if(error) { NSLog(@"json解析失敗:%@",error); return nil; } return dic; }
+ (NSString*)dictionaryToJson:(NSDictionary *)dic { NSError *error = nil; NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dic
options:NSJSONWritingPrettyPrinted
error:&error]; return [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding]; }