2015-07-03 21 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