2016-05-09 9 views
0

私の製品jsonにこの機能があります。それは単に文字列のリストです。しかし、それらをローカライズする必要があります。したがって、配列の配列になります。レルムの目的c - 配列の配列

領域の配列を配列できません。誰でも私にRealm Objective-Cでこれを達成する方法を教えてもらえますか?

{ 
"name" : "Product 1", 
"features" : [ 
[ 
    { "locale" : "en", value : "feature1"}, 
    { "locale" : "cn", value : "feature1 in cn"} 
], 
[ 
    { "locale" : "en", value : "feature2"}, 
    { "locale" : "cn", value : "feature2 in cn"} 
] 
] 
} 

ソースで、またはあなたのコード内のマッピングステップの一部として、少しあなたのJSONを変更することによりおかげ

答えて

2

を、あなたはレルムのKVCの初期化機構に直接渡すことができます:

{ 
    "name": "Product 1", 
    "features": [ 
    [[ 
     { "locale": "en", "value": "feature1" }, 
     { "locale": "cn", "value": "feature1 in cn" } 
    ]], 
    [[ 
     { "locale": "en", "value": "feature2" }, 
     { "locale": "cn", "value": "feature2 in cn" } 
    ]] 
    ] 
} 
これらのレルムモデルにマップ

:この時点で

@interface Feature : RLMObject 
@property NSString *locale; 
@property NSString *value; 
@end 
@implementation Feature 
@end 

RLM_ARRAY_TYPE(Feature); 

@interface FeatureList : RLMObject 
@property RLMArray<Feature> *features; 
@end 
@implementation FeatureList 
@end 

RLM_ARRAY_TYPE(FeatureList); 

@interface Product : RLMObject 
@property NSString *name; 
@property RLMArray<FeatureList> *features; 
@end 
@implementation Product 
@end 

は、あなたがにJSONをデシリアライズすることができますこれで辞書と初期化し、あなたのレルムのオブジェクトグラフ:

NSDictionary *productDictionary = [NSJSONSerialization JSONObjectWithData:[NSData dataWithContentsOfURL:[[NSBundle mainBundle] URLForResource:@"product" withExtension:@"json"]] options:0 error:nil]; 
[Product createInDefaultRealmWithValue:productDictionary]; 

あなたの次のオブジェクトグラフを与える:

[0] Product { 
    name = Product 1; 
    features = RLMArray <0x7fe43366be00> (
    [0] FeatureList { 
     features = RLMArray <0x7fec3a772c10> (
     [0] Feature { 
      locale = en; 
      value = feature1; 
     }, 
     [1] Feature { 
      locale = cn; 
      value = feature1 in cn; 
     } 
    ); 
    }, 
    [1] FeatureList { 
     features = RLMArray <0x7fec3a773d20> (
     [0] Feature { 
      locale = en; 
      value = feature2; 
     }, 
     [1] Feature { 
      locale = cn; 
      value = feature2 in cn; 
     } 
    ); 
    } 
); 
} 
私はJSONでfeatureListsを置くべき
+0

? –

+0

ご質問の最新の編集内容を反映するように回答を更新しました – jpsim

+0

jsonではなくRealmモデル側を変更できますか? –