0

我想知道是否有人可以提供一些帮助。基本上我正在调用网络服务,然后尝试获取大型托管图像 url。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?

非常感谢您提供的任何帮助!

4

3 回答 3

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]);
于 2013-04-28T18:23:10.617 回答
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"];
于 2013-04-28T18:34:59.580 回答
0

的值[imageArray objectAtIndex:0]是一个 NSDictionary。您错误地将其指定为 NSString。您需要以下内容:

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

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

   hostedLarguerDictionary[@"hostedLargeUrl"]

或者,等效地

   [hostedLarguerDictionary objectForKey: @"hostedLargeUrl"];
于 2013-04-28T18:37:18.110 回答