Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 检查字典中的数组并返回字典的UIImage_Arrays_Swift_Dictionary - Fatal编程技术网

Arrays 检查字典中的数组并返回字典的UIImage

Arrays 检查字典中的数组并返回字典的UIImage,arrays,swift,dictionary,Arrays,Swift,Dictionary,我有一个字典示例: (一) var dict=[[“A”:UIImage(名为:“A.png”),“B”:UIImage(名为:“B.png”),“C”:UIImage(名为:“C.png”)]//迭代该数组,并收集与另一个数组相关联的图像 var images = [UIImage]() for key in array { if let image = dict[key] { images.append(image) } } /

我有一个字典示例: (一)


var dict=[[“A”:UIImage(名为:“A.png”),“B”:UIImage(名为:“B.png”),“C”:UIImage(名为:“C.png”)]
//迭代该数组,并收集与另一个数组相关联的图像

 var images = [UIImage]()
    for key in array {
       if let image = dict[key] {
          images.append(image)
    }
    }
//图像现在将按数组中指定的键的顺序包含图像

编辑

如果您希望甚至nil项都在数组中,那么您需要一个可选图像对象数组

 var images = [UIImage?]()
    for key in array {
          images.append(dict[key])
    }

  //While fetching the items, ensure you unwrap them-
  //for example- the following tries to fetch and unwrap images from this array
   for image in images {
      if let image = image {
        //Do something with the image
      }else {
        //The image was not present.
      }
   } 

谢谢你的回答!我将有一个UIImage?的数组,带有打印(“(图像)”)不,它将由“if let image”展开,您将拥有实际的图像对象。我没有尝试过,但它的顺序不同。这是怎么可能的?相同的顺序是什么意思?@Mams1101?它应该按照数组中给定的键的顺序对吗?是的,这是我想要的,与var数组的顺序相同,我如何检查dictionary的键是否等价于带有查询的数组?