【问题标题】:Separate two strings from one array element从一个数组元素中分离两个字符串
【发布时间】:2013-04-28 18:15:57
【问题描述】:

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

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

【问题讨论】:

    标签: ios objective-c json nsstring nsarray


    【解决方案1】:

    实际上图像数组包含一个字典

    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"];
    

    【讨论】:

    • ...以及下面的所有人。我可能没有尝试过您的答案,但非常感谢您的帮助。
    • 还有一件事。有没有一种方法可以让我基本上检查变量所持有的数据类型?是否有某种“这是一种”方法可以在这种情况下提供帮助?
    【解决方案2】:

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

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

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

       hostedLarguerDictionary[@"hostedLargeUrl"]
    

    或者,等价

       [hostedLarguerDictionary objectForKey: @"hostedLargeUrl"];
    

    【讨论】:

      【解决方案3】:

      看起来像一个数组,所以

      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]);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-05-18
        • 1970-01-01
        • 2021-04-07
        • 2019-03-09
        • 2016-07-05
        • 2021-10-15
        • 2020-05-29
        • 2023-03-04
        相关资源
        最近更新 更多