2013-04-28 42 views
0

我想知道是否有人可以提供一些帮助。基本上我打电话给一个网络服务,然后尝试获取大的托管图像网址。从Web服务的输出是这样:从一个数组元素中分离两个字符串

images =  (
       { 
      hostedLargeUrl = "http://i.yummly.com/Crispy-roasted-chickpeas-_garbanzo-beans_-308444.l.jpg"; 
      hostedSmallUrl = "http://i.yummly.com/Crispy-roasted-chickpeas-_garbanzo-beans_-308444.s.jpg"; 
     } 
    ); 

的主要问题是两个字符串中只有我的数组元素之一,当我想他们应该是2。此外我不是100%,但有可能他们可能是一本字典:-S我只是不确定。我的代码如下:

NSArray *imageArray = [[NSArray alloc]init]; 
    imageArray = [self.detailedSearchYummlyRecipeResults objectForKey:@"images"]; 
    NSLog(@"imageArray: %@", imageArray); 
    NSLog(@"count imageArray: %lu", (unsigned long)[imageArray count]); 
    NSString *hostedLargeurlString = [imageArray objectAtIndex:0];  
    NSLog(@"imageArrayString: %@", hostedLargeurlString); 

从上面的代码的输出(的NSLog的)是:

2013-04-28 18:59:52.265 CustomTableView[2635:11303] imageArray: (
     { 
     hostedLargeUrl = "http://i.yummly.com/Crispy-roasted-chickpeas-_garbanzo-beans_-308444.l.jpg"; 
     hostedSmallUrl = "http://i.yummly.com/Crispy-roasted-chickpeas-_garbanzo-beans_-308444.s.jpg"; 
    } 
) 
2013-04-28 18:59:52.266 CustomTableView[2635:11303] count imageArray: 1 
2013-04-28 18:59:52.266 CustomTableView[2635:11303] imageArrayString: { 
    hostedLargeUrl = "http://i.yummly.com/Crispy-roasted-chickpeas-_garbanzo-beans_-308444.l.jpg"; 
    hostedSmallUrl = "http://i.yummly.com/Crispy-roasted-chickpeas-_garbanzo-beans_-308444.s.jpg"; 
} 

没有人有任何想法如何,我可以分别对一个元素单独成hostedlargeUrl和hostedsmallUrl?

任何帮助,您可以提供非常感谢!

回答

0

实际上图像数组包含字典

images = ( 
     { 
      hostedLargeUrl = "http://i.yummly.com/Crispy-roasted-chickpeas-_garbanzo-beans_-308444.l.jpg"; 
      hostedSmallUrl = "http://i.yummly.com/Crispy-roasted-chickpeas-_garbanzo-beans_-308444.s.jpg"; 
     } 
); 

这样:

NSDictionary *d = [self.detailedSearchYummlyRecipeResults objectForKey:@"images"][0]; 
NSString *largeURL = d[@"hostedLargeUrl"]; 
NSString *smallURL = d[@"hostedSmallUrl"]; 
+0

非常感谢我想这和它的工作。 – Anthony 2013-04-29 06:09:53

+0

......以及以下所有人。我可能没有尝试过你的答案,但你的帮助真的很感激。 – Anthony 2013-04-29 06:11:34

+0

还有一件事。有没有一种方法可以基本检查变量所保存的数据类型?是否有某种“这是一种”方法可以在这种情况下提供帮助? – Anthony 2013-04-29 06:21:19

0

看起来像一个数组,数组中的那么

NSArray* links = [self.detailedSearchYummlyRecipeResults objectForKey:@"images"]; 
NSString* bigLink = [links objectAtIndex:0]; 
NSString* smallLink = [links objectAtIndex:1]; 

,或者它可能是一个字典

NSDictionary* links = [self.detailedSearchYummlyRecipeResults objectForKey:@"images"]; 
    NSString* bigLink = [links objectForKey:@"hostedLargeUrl "]; 
    NSString* smallLink = [links objectForKey:@"hostedSmallUrl "]; 

您可以通过打印类名

看到该对象的类别的
NSLog(@"Class Type: %@", [[self.detailedSearchYummlyRecipeResults objectForKey:@"images"] class]); 
0

[imageArray objectAtIndex:0]的值是一个的NSDictionary。你错误地将它指定为NSString。您需要:

NSDictionary *hostedLarguerDictionary = 
    (NSDictionary *) [imageArray objectAtIndex:0]; 

,然后访问“大网址”使用:

hostedLarguerDictionary[@"hostedLargeUrl"] 

,或等效

[hostedLarguerDictionary objectForKey: @"hostedLargeUrl"]; 
相关问题