[英]How can I alphabetically sort the values in my NSDictionary with an NSArray?
[英]MOTIS Object Mapping, With NSDictionary with values NSArray how can I specify type of array elements?
我有json
{“類型”:{“食物”:[{“cve”:“1”,“description”:“Pizza”},{“cve”:“2”,“description”:“Restaurant”},{“cve “:”3“,”description“:”Cafe“}],”Health“:[{”cve“:”3“,”description“:”Pharmacy“},{”cve“:”4“,”description “:“醫院”}] } }
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獲得這些
據我所知,您的Types
對象未正確定義。 如果您具有NSDictionary*
類型的屬性並且收到的JSON是字典,則Motis將不會執行任何自動轉換,因為類型已經匹配(您正在接收字典,並且您的屬性是NSDictionary類型)。
因此,您必須遵循JSON結構實現Type
對象。 這意味着您的Type
對象必須具有兩個類型為array的屬性,一個用於food
,一個用於health
。 然后,使用方法+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
的實現,你的已經是正確的了。 但是,您不應該使用屬性名稱description
因為它已被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];
希望這可以解決您的問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.