简体   繁体   English

Int&#39;不能转换为&#39;Range <Int>

[英]Int' is not convertible to 'Range<Int>

I am trying to create a dictionary containing four images to set one of them as background of the UIView . 我正在尝试创建一个包含四个图像的字典,以将其中一个设置为UIView背景。 The image selected will depend on the value of a rand number. 选择的图像取决于rand数的值。

Here is code that creates a dictionary: 这是创建字典的代码:

let backgroundDict = [
        "Clouds":   UIImage(named: "clouds.jpg"),
        "Clouds2":  UIImage(named: "clouds2.jpg"),
        "Sunset":   UIImage(named: "sunset.jpg"),
        "Sunset2":  UIImage(named: "sunset2.jpg")
    ]

Here is how I create the rand number: 这是我如何创建rand数:

var randNumber = Int(arc4random_uniform(UInt32(backgroundDict.count)))

This is the code that declares an array with the keys of the dictionary: 这是用字典的键声明数组的代码:

let backgroundArray = Array(backgroundDict.keys)

I get the error 'Int' is not convertible to 'Range<Int> when I try to set the background image: 尝试设置背景图像时,出现错误'Int' is not convertible to 'Range<Int>的错误:

self.view.backgroundColor = UIColor(patternImage: backgroundDict[backgroundArray[randNumber] as String])

Why is that happening? 为什么会这样呢?

There are 2 similar problems: 有两个类似的问题:

  • the UIImage initializer is failable, so it returns an optional UIImage初始化程序失败,因此它返回一个可选
  • the dictionary always returns an optional when accessing an element by key 通过键访问元素时,字典始终返回可选值

There are 2 ways to fix it: 有两种解决方法:

  • force unwrapping the image twice: 强制展开图像两次:

     UIColor(patternImage: backgroundDict[backgroundArray[randNumber] as String]!!) ^^ 
  • force unwrap images when creating the dictionary: 创建字典时强制解开图像:

     let backgroundDict = [ "Clouds": UIImage(named: "clouds.jpg")!, "Clouds2": UIImage(named: "clouds2.jpg")!, "Sunset": UIImage(named: "sunset.jpg")!, "Sunset2": UIImage(named: "sunset2.jpg")! ] 

    then use a single force unwrap when accessing the dictionary by value: 然后在按值访问字典时使用单力解开:

     UIColor(patternImage: backgroundDict[backgroundArray[randNumber] as String]!) 

Although I'd usually recommend using optional binding to prevent runtime exceptions, I presume that in this case images must exist, and if they don't it is a development mistake (ie failing to add the images to the project or mistyping their names) - so an exception is a great way to figure that out. 尽管我通常建议使用可选绑定来防止运行时异常,但我认为在这种情况下图像必须存在,如果不存在,则是开发错误(即无法将图像添加到项目中或错误地命名其名称) -因此,例外是解决此问题的好方法。

UIImage(named: "") is an optional initializer init? UIImage(named: "")是可选的初始化程序init? and UIColor(patternImage: ...) takes a UIImage, so you need to unwrap your images before using them. UIColor(patternImage: ...)需要一个UIImage,因此您需要在使用图像之前先对其进行包装。 You also need to unwrap the value returned from the dictionary: 您还需要解开字典返回的值:

//In Swift 1.2
if let optionalImage = backgroundDict[backgroundArray[randNumber] as String], image = optionalImage {
         UIImage(patternImage: image)
}

//In Swift 1.1
if let optionalImage = backgroundDict[backgroundArray[randNumber] as String] {  
     if let image = optionalImage {
         UIImage(patternImage: image)
     }
}

Dictionaries return optionals in swift to handle the case of non-existing keys. 字典会迅速返回可选内容,以处理不存在的键的情况。 Using if let image = backgroundDict[backgroundArray[randNumber]] makes sure there's a corresponding UIImage for the key backgroundArray[randNumber] before assigning to the variable image . 使用if let image = backgroundDict[backgroundArray[randNumber]]可以确保在分配给变量image之前,键backgroundArray[randNumber]有对应的UIImage。

The image! image! (optional unwrapping) inside the conditional is used because image is of type UIImage? (可选的展开条件),因为image的类型为UIImage? (this will cause an error if it's nil). (如果为零,则将导致错误)。 Initialising UIImage by name returns an optional to handle the case of not finding an image with the name provided. 通过名称初始化UIImage返回一个可选参数,以处理找不到带有提供名称的图像的情况。

let backgroundDict = [
            "Clouds":   UIImage(named: "clouds.jpg"),
            "Clouds2":  UIImage(named: "clouds2.jpg"),
            "Sunset":   UIImage(named: "sunset.jpg"),
            "Sunset2":  UIImage(named: "sunset2.jpg")
        ]

        var randNumber = Int(arc4random_uniform(UInt32(backgroundDict.count)))
        let backgroundArray = Array(backgroundDict.keys)

        if let image = backgroundDict[backgroundArray[randNumber]] {
            self.view.backgroundColor = UIColor(patternImage: image!)
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM