2015-07-03 19 views
-2

我有2 NSArray與內容ID和其他內容的URL與NSDictionary的合作,陣列

但是,當我創建NSDictionary它的外觀像這樣(從NSLog):

2015-07-03 17:10:51.072 hibridTesting[4950:166675] { 
    (
) =  (
); 
    (
    30, 
    31 
) =  (
    "https://www.google.com", 
    "https://www.yahoo.com" 
); 
    (
    10, 
    11, 
    12, 
    13 
) =  (
    "https://www.facebook.com/", 
    "https://www.sapo.pt", 
    "https://www.sapo.pt", 
    "https://www.sapo.pt" 
); 
    (
    20, 
    21, 
    22, 
    23, 
    24, 
    25 
) =  (
    "https://www.google.com", 
    "https://www.google.com", 
    "https://www.google.com", 
    "https://www.google.com", 
    "https://www.google.com", 
    "https://www.google.com" 
); 
} 

,如果我這樣做這

arrayDeSitesSubmenus = [mydictionary objectForKey:@"21"]; 

,如果打印我arrayDeSitesSubmenus它說nil

我想是每個ID的網址,以及我的理解是關鍵的小組,一組網址

編輯:

我的日誌從陣列是:

2015-07-03 17:33:55.771 hibridTesting[5122:174427] (
    (
    "https://www.facebook.com/", 
    "https://www.sapo.pt", 
    "https://www.sapo.pt", 
    "https://www.sapo.pt" 
), 
    (
    "https://www.google.com", 
    "https://www.google.com", 
    "https://www.google.com", 
    "https://www.google.com", 
    "https://www.google.com", 
    "https://www.google.com" 
), 
    (
    "https://www.google.com", 
    "https://www.yahoo.com" 
), 
    (
), 
    (
), 
    (
), 
    (
) 
) 
2015-07-03 17:33:55.771 hibridTesting[5122:174427] (
    (
    10, 
    11, 
    12, 
    13 
), 
    (
    20, 
    21, 
    22, 
    23, 
    24, 
    25 
), 
    (
    30, 
    31 
), 
    (
), 
    (
), 
    (
), 
    (
) 
) 

我把我的數組從xmlparse,這就是爲什麼我的日誌看起來像 謝謝。

+1

您沒有正確創建您的'NSDictionary'。關鍵不是@「21」,而是從20到25的「NSArray」和相應的URL。使用這個:'[NSDictionary dictionaryWithObjects:arrayURLs forKeys:arrayIds];' – Larme

回答

0

你是如何創建你的NSDictionary

當創建的NSDictionary你有兩個選擇:

第一:

NSDictionary *dictionary = [[NSDictionary alloc] initWithObjectsAndKeys:@"value1", @"key1", nil]; 

另一個是

NSDictionary *mdictionary = @{ @"key1" : @"value1" }; 

既然你已經有兩個鍵和值的陣列,最好的一個因爲您使用的是initWithObjects:forKeys,它接受數組和值的排列並進行相應的排列。

NSArray *keys = @[@"1", @"2", @"3", @"4", @"5"]; 

NSArray *values = @[@"value1", @"value2", @"value3", @"value4", @"value5"]; 

NSDictionary *dictionary = [[NSDictionary alloc] initWithObjects:values forKeys:keys]; 

和輸出將是:

{ 
    1 = value1; 
    2 = value2; 
    3 = value3; 
    4 = value4; 
    5 = value5; 
} 

但如果你的鑰匙像

NSArray *keys = @[@1, @2, @3, @4, @5]; 

NSArray *values = @[@"value1", @"value2", @"value3", @"value4", @"value5"]; 

NSDictionary *dictionary = [[NSDictionary alloc] initWithObjects:values forKeys:keys]; 

//This is wrong and will return (null) 
// 
//NSLog(@"%@", [dictionary objectForKey:@"1"]); 

The correct one is: 
NSLog(@"%@", [dictionary objectForKey:@1]); 
or 
NSLog(@"%@", [dictionary objectForKey:[NSNumber numberWithInt:1]]); 

希望的數字,這是對您有所幫助。乾杯!

+0

嗨,謝謝隊友,尋求幫助。但已經解決了我想抱歉打擾你,它是不好的邏輯,即時通訊新的編程,和客觀的C是好心的,我嘗試其他認爲,在我的XML我有menuIDs,所以這是更容易工作,現在我有id1 4urls id2 6urls等...所以知道我可以做我想做的事 –

+0

好吧,那很好.. :) – 0yeoj