【问题标题】:objective c - how to get elements dynamically from NSArray? [closed]目标 c - 如何从 NSArray 动态获取元素? [关闭]
【发布时间】:2013-11-21 15:07:15
【问题描述】:

我有下面的 json 文件,我通过这样做将它保存在一个数组中

NSArray *allFrames = [self.campaign.form.gameParams objectForKey:@"frames"];

我想要实现的是将url中的所有水平图像放在一个视图中。我的问题是我想动态执行此操作,因为元素的数量可能会发生变化。

  frames =     (
            {
        horizontal = "http://mysite.co.uk/files/frames/5_h.png";
        vertical = "http://mysite.co.uk/files/frames/5_v.png";
    },

            {
        horizontal = "http://mysite.co.uk/files/frames/6_h.png";
        vertical = "http://mysite.co.uk/files/frames/6_v.png";
    }
            {
        horizontal = "http://mysite.co.uk/files/frames/6_h.png";
        vertical = "http://mysite.co.uk/files/frames/6_v.png";
    }
);

到目前为止我所做的是像这样静态存储网址

    NSDictionary *frames = allFrames[0];

    NSString * link1 = [frames objectForKey:@"horizontal"];


    NSDictionary *frames = allFrames[1];

    NSString * link2 = [frames objectForKey:@"horizontal"];


    NSDictionary *frames = allFrames[2];

    NSString * link3 = [frames objectForKey:@"horizontal"];

然后将网址转换成图片

    UIImage *webImage1 = [UIImage imageWithData:
                                   [NSData dataWithContentsOfURL:
                                    [NSURL URLWithString:link1]]]; 
                                                               etc

任何想法如何做到这一点?

【问题讨论】:

  • 遍历数组...
  • 你能解释一下吗?我是新手
  • 我简直不敢相信谷歌没有想出一些有用的东西。这个问题重复了很多次...

标签: ios objective-c nsarray nsdictionary


【解决方案1】:

这样的?

NSMutableArray *imageArray = [[NSMutableArray alloc] init];

for (int i = 0; i < allFrames.count; i++) {
    NSDictionary *dict = allFrames[i];
    NSString *link = dict[@"horizontal"];

    UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:link]]];

    [imageArray addObject:image];
}

【讨论】:

    【解决方案2】:

    可以使用 NSArray 的 valueForKey 过滤水平数据。

    NSArray *horizontalImages = [frames valueForKey:@"horizontal"];
    
    for(NSString *urlString in horizontalImages)
    {
    UIImage *webImage1 = [UIImage imageWithData:
                                       [NSData dataWithContentsOfURL:
                                        [NSURL URLWithString: urlString]]];
    }
    

    【讨论】:

      【解决方案3】:
      NSArray *dict = @[@{@"horizontal": @"http://mysite.co.uk/files/frames/5_h.png"}, @{@"horizontal": @"http://mysite.co.uk/files/frames/6_h.png"}, @{@"horizontal": @"http://mysite.co.uk/files/frames/6_h.png"}];
      NSMutableArray *horizontalLinks = [NSMutableArray array];
      [dict enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
          [horizontalLinks addObject:[obj objectForKey:@"horizontal"]];
      }];
      
      NSLog(@"Horizontal Links: %@", horizontalLinks);
      

      会给你:

      Horizontal Links: (
          "http://mysite.co.uk/files/frames/5_h.png",
          "http://mysite.co.uk/files/frames/6_h.png",
          "http://mysite.co.uk/files/frames/6_h.png"
      )
      

      【讨论】:

        猜你喜欢
        • 2014-09-11
        • 1970-01-01
        • 2021-12-03
        • 2017-04-18
        • 2023-04-01
        • 2011-12-11
        • 2018-09-19
        • 2018-11-29
        • 1970-01-01
        相关资源
        最近更新 更多