大佬教程收集整理的这篇文章主要介绍了ios – MOTIS对象映射,使用带有NSArray值的NSDictionary,如何指定数组元素的类型?,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
types.h中
#import <Foundation/Foundation.h> @interface Types: NSObject @property (nonatomic,copy) NSDictionary *types; @end
Types.m
#import "Types.h" #import <Motis/Motis.h> #import "SubTipo.h" @implementation Types + (NSDictionary*)mts_mapping { return @{@"types": mts_key(types),}; } @end
Subtype.h
#import <Foundation/Foundation.h> @interface Subtype: NSObject @property (nonatomic,assign) int cve; @property (nonatomic,copy) NSString *description; @end
Subtype.m
#import "Subtype.h" #import <Motis/Motis.h> @implementation Subtype + (NSDictionary*)mts_mapping { return @{@"cve": mts_key(cvE),@"description": mts_key(description),}; } @end
我用反序列化
Types * values=[[Types alloc]init]; NSDictionary * jsonObject = [NSJSONserialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error]; [values mts_SETVALuesForKeysWithDictionary:jsonObject ];
我用NSDictionary的NSArray获得了NSDictionary
但是我需要带有NSArray子类型的NSDictionary
我试着用
+ (NSDictionary*)mts_arrayClassmapping { return @{mts_key(types): Subtype.class}; }
但没有成功
我如何通过Motis获得这些
因此,您必须遵循JSON结构实现Type对象.这意味着您的Type对象必须具有两个类型为array的属性,一个用于food,一个用于health.然后,使用@L_142_8@mts_arrayClassmapping,您可以将数组的内容类型指定为Subtype.
这里实施:
// ***** Type.h file ***** // @interface Type: NSObject @property (nonatomic,strong) NSArray *food; @property (nonatomic,strong) NSArray *health; @end // ***** Type.m file ***** // @implementation Type + (NSDictionary*)mts_mapping { return @{@"food": mts_key(food),@"Health": mts_key(health),}; } + (NSDictionary*)mts_arrayClassmapping { return @{mts_key(food): Subtype.class,mts_key(health): Subtype.class,}; } @end
关于Subtype的实现,你的已经是正确的了.但是,您不应该使用属性名称描述,因为它已被NSObject使用:
// ***** Subtype.h file ***** // @interface Subtype: NSObject @property (nonatomic,assign) NSInteger cve; @property (nonatomic,copy) NSString *theDescription; @end // ***** Subtypes.m file ***** // @implementation Subtype + (NSDictionary*)mts_mapping { return @{@"cve": mts_key(cvE),@"description": mts_key(theDescription),}; } @end
最后,如上所示,您可以映射您的JSON,但首先您必须提取关键类型的“字典”,您将映射到“类型”模型对象.
// Get the json data NSDictionary * jsonObject = [NSJSONserialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error]; // Extract the JSON Dictionary of types. NSDictionary *jsonType = [jsonObject objectForKey:@"Types"]; // Create a Type object Type *type = [[Type alloc] init]; // Map JSON contents to the type object with Motis [type mts_SETVALuesForKeysWithDictionary:jsonType];
希望这可以解决您的问题.
以上是大佬教程为你收集整理的ios – MOTIS对象映射,使用带有NSArray值的NSDictionary,如何指定数组元素的类型?全部内容,希望文章能够帮你解决ios – MOTIS对象映射,使用带有NSArray值的NSDictionary,如何指定数组元素的类型?所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。