2016-04-05 18 views
2

私はNSMutableArrayのように見えます。このArrayを別のViewControllerに渡して、その要素を使用したいと思います。しかし、私は各要素にキーを追加する方法について混乱しています。たとえば、{@"bus_route_key":@"733"}NSMutableArrayにキーを追加する方法

キーで配列を初期化する方法はありますか?それともNSDictionaryを作成する必要がありますか?

@[ 
@[ 
    @733, 
    @"Oakleigh - Box Hill via Clayton & Monash University & Mt Waverley", 
    @"Oakleigh Railway Station/Johnson St", 
    @"2016-04-05T11:44:00Z" 
    ], 
@[ 
    @631, 
    @"Southland - Waverley Gardens via Clayton & Monash University", 
    @"Southland Shopping Centre/Karen St", 
    @"2016-04-05T11:46:00Z" 
    ], 
@[ 
    @703, 
    @"Middle Brighton - Blackburn via Bentleigh & Clayton & Monash University (SMARTBUS Service)", 
    @"Luntar Rd/Centre Rd", 
    @"2016-04-05T11:50:00Z" 
    ] 
]; 
+4

はい辞書を使用します。配列にはキーがなく、インデックスだけがあります。 – trojanfoe

答えて

-1
NSArray *mainArray = @[ 
@[ 
    @733, 
    @"Oakleigh - Box Hill via Clayton & Monash University & Mt Waverley", 
    @"Oakleigh Railway Station/Johnson St", 
    @"2016-04-05T11:44:00Z" 
    ], 
@[ 
    @631, 
    @"Southland - Waverley Gardens via Clayton & Monash University", 
    @"Southland Shopping Centre/Karen St", 
    @"2016-04-05T11:46:00Z" 
    ], 
@[ 
    @703, 
    @"Middle Brighton - Blackburn via Bentleigh & Clayton & Monash University (SMARTBUS Service)", 
    @"Luntar Rd/Centre Rd", 
    @"2016-04-05T11:50:00Z" 
    ] 
]; 

NSMutableArray *newArray = [NSMutableArray array]; 

for (NSArray *arr in mainArray) { 

     NSDictionary *dict = @{ 
           @"key1" : [arr objectAtIndex:0], 
           @"key2" : [arr objectAtIndex:1], 
           @"key3" : [arr objectAtIndex:2] 
           }; 

    [newArray addObject:dict]; 
} 

DLOG(@"New Arr : %@", newArray); 

//you can access by key like this 

for (NSDictionary *dict in newArray) { 

    DLOG(@"%@", [dict valueForKey:@"key1"]); 
    DLOG(@"%@", [dict valueForKey:@"key2"]); 
    DLOG(@"%@", [dict valueForKey:@"key3"]); 
} 

はそれが..あなたを助けていただければ幸いした値:)以下

+0

魔法のように働く、まさに私が欲しいものです。あなたの助けをありがとう! –

1

うわー、ねえ、そこにメルボルン。あなたのNSMutableArrayNSArraysが含まれている、現時点で

(
    { 
    "route": 733, 
    "line_name": "Oakleigh - Box Hill via Clayton & Monash University & Mt Waverley", 
    "stop": "Oakleigh Railway Station/Johnson St", 
    "time": "2016-04-05T11:44:00Z" 
    }, 

    //Insert other line dictionaries 
) 
+0

ハッ、メルボルンコーダーです。ありがとうございました、私は既に配列の辞書を追加しました! –

0

あなたはより構造化されたことをNSMutableDictionariesとセットアップ各バスラインが含まれているNSMutableArrayのを持っている必要があります。あなたの場合、これらのNSArrayNSDictionary秒またはNSMutableDictionary秒である必要があります。

次に、あなたは、このような最初の要素の例については、bus_route_keyを編集することができます。 [[yourList objectAtIndex:0] setInteger:1337 forKey:@"bus_route_key"] あなたは文字列を設定したい場合、あなたはdoesnの配列としてごNSArrayNSDictionaryに変換することができsetObject

0

を使用する必要があります」 Tキーを含み、配列はインデックスのみ

+0

これはコメントではありません。 –

-1

使用コード行:
NSMutableDictionary * dict2 = [[NSMutableDictionary alloc] initWithObjects:attributeValues for Keys:attributeKeys];

ここで、attributeValuesは値NSDictionaryNSArrayで、attributeKeysはキーのNSArrayです。

は、参照リンクの下に参照してください。

http://hayageek.com/nsdictionary-nsmutabledictionary/

NSString * key1 [email protected]"name"; 
NSString * obj1 [email protected]"Hayagreeva"; 
NSString * key2 [email protected]"age"; 
NSNumber * obj2 =[NSNumber numberWithInt:3]; 

//1.Empty dictionary. @{} dictionary representation and it is not for NSMuableDictionary 
NSDictionary * dict1 =[NSDictionary dictionary]; 
NSDictionary * dict2 =[[NSDictionary alloc] init]; 
NSDictionary * dict3 = @{}; 

//2.Dictionary with Single Object and Key 
NSDictionary * dict4 =[NSDictionary dictionaryWithObject:obj1 forKey:key1]; 
NSDictionary * dict5 [email protected]{key1:obj1}; 

//3.Dictionary with Multiple Object and Keys 
//Objects and keys are sequentially terminated with nil 
NSDictionary * dict6 = [NSDictionary 
         dictionaryWithObjectsAndKeys:obj1,key1,obj2,key2,nil]; 

NSDictionary * dict7 =[[NSDictionary alloc] 
         initWithObjectsAndKeys:obj1,key1,obj2,key2,nil] ; 

//3.Dictionary with Multiple Object and Keys 
NSDictionary *dict8 =[NSDictionary 
         dictionaryWithObjects:@[obj1,obj2] 
         forKeys:@[key1,key2]]; 
NSDictionary *dict9 =[[NSDictionary alloc] 
         initWithObjects:@[obj1,obj2] 
         forKeys:@[key1,key2]]; 
NSDictionary * dict10 [email protected]{key1:obj1,key2:obj2}; 

//@[obj1,obj2] is the array representation. dict6,dict7,dict8 are same. 
NSDictionary *dict11 =[[NSDictionary alloc] 
         initWithObjects:[NSArray arrayWithObjects:obj1,obj2,nil] 
         forKeys:[NSArray arrayWithObjects:key1,key2,nil]]; 

//4.Dictionary with another Dictionary 
//@[obj1,obj2] is the array representation. dict6,dict7,dict8 are same. 
NSDictionary *dict12=[NSDictionary dictionaryWithDictionary:dict8]; 
NSDictionary *dict13=[[NSDictionary alloc] initWithDictionary:dict8]; 
0

私のコードがお手伝いします:)

NSMutableArray *arrTitles = (NSMutableArray *)@[@{ 
    "route": 733, 
    "line_name": "Oakleigh - Box Hill via Clayton & Monash University & Mt Waverley", 
    "stop": "Oakleigh Railway Station/Johnson St", 
    "time": "2016-04-05T11:44:00Z" 
     },@{ 
    "route": 733, 
    "line_name": "Oakleigh - Box Hill via Clayton & Monash University & Mt Waverley", 
    "stop": "Oakleigh Railway Station/Johnson St", 
    "time": "2016-04-05T11:44:00Z" 
    }]; 
関連する問題