【问题标题】:Swift 2.2: Using GKRandomSource with an array of UIImagesSwift 2.2:使用带有 UIImages 数组的 GKRandomSource
【发布时间】:2017-04-12 20:55:28
【问题描述】:

我正在尝试通过执行以下操作来随机化 UIImages 数组:

import UIKit
import GameplayKit

//Create an array of the pictures. They are already in xcassets.

var picturePieces = [UIImage(named: "concordTL"), 
                     UIImage(named: "concordTC"), 
                     UIImage(named: "concordTR"), 
                     UIImage(named: "concordLC"), 
                     UIImage(named: "concordC"), 
                     UIImage(named: "concordRC"), 
                     UIImage(named: "concordBL"), 
                     UIImage(named: "concordBC"), 
                     UIImage(named: "concordBR")]

//Randomizer function using GKRandomSource


func shuffle() {
var shuffledPicturePieces = GKRandomSource.sharedRandom().arrayByShufflingObjectsInArray(picturePieces)

此处:我在 (picturePieces) 处不断收到错误“无法将类型 '[UIImage?]' 的值转换为预期的参数类型 '[AnyObject]'”。

GKRandomSource 不能与 UIImage 一起使用吗?

然后我想将 shuffledPicturePieces 中的每个随机 UIImage 分配给我的每个 @IBOutlets,如下所示:

//Outlets for ImageViews.

@IBOutlet weak var EasyTopLeft: UIImageView!

@IBOutlet weak var EasyTopCenter: UIImageView!

@IBOutlet weak var EasyTopRight: UIImageView!

@IBOutlet weak var EasyLeftCenter: UIImageView!

@IBOutlet weak var EasyCenter: UIImageView!

@IBOutlet weak var EasyRightCenter: UIImageView!

@IBOutlet weak var EasyBottomLeft: UIImageView!

@IBOutlet weak var EasyBottomCenter: UIImageView!

@IBOutlet weak var EasyBottomRight: UIImageView!

//Method to get a picture for the puzzle.

@IBAction func getPictureButton(sender: UIButton) {
    shuffle()

    if EasyTopLeft == nil{
        EasyTopLeft.image = shuffledPicturePieces[Int[0]]
        EasyTopCenter.image = shuffledPicturePieces[Int[1]]
        EasyTopRight.image = shuffledPicturePieces[Int[2]]
        EasyLeftCenter.image = shuffledPicturePieces[Int[3]]
        EasyCenter.image = shuffledPicturePieces[Int[4]]
        EasyRightCenter.image = shuffledPicturePieces[Int[5]]
        EasyBottomLeft.image = shuffledPicturePieces[Int[6]]
        EasyBottomCenter.image = shuffledPicturePieces[Int[7]]
        EasyBottomRight.image = shuffledPicturePieces[Int[8]]
    }


    else{
        NSLog("Image already loaded!")
    }



}

此处:对于 IF 语句中的每个项目,我不断收到错误“使用未解析的标识符 'shuffledPicturePieces'”。

感谢您的帮助!

-弗兰克

【问题讨论】:

    标签: ios arrays swift swift2 uiimage


    【解决方案1】:

    你的第一个错误:

    无法将类型“[UIImage?]”的值转换为预期的参数类型“[AnyObject]”

    发生是因为您将 Optional 类型的数组传递给需要非可选类型数组的函数。由于您“知道”这些图像存在于您的资产目录中,因此使它们成为非可选的一种简单方法是强制打开它们(注意添加的 !):

    var picturePieces = [UIImage(named: "concordTL")!, 
                         UIImage(named: "concordTC")!,
                         // ...
    

    或者,您可以使用函数式编程风格更明确地处理错误:

    let imageNames = ["concordTL", "concordTC", /*...*/]
    let picturePieces = imageNames.map { name in
        guard let image = UIImage(named: name) 
            else { fatalError("missing from asset catalog: \(name)") }
        return image
    }
    

    请注意,一旦你有一个图像数组 ([UIImage]),你可以将它传递给 GKRandomSource.arrayByShufflingObjectsInArray,但是经过洗牌的数组会丢失元素类型信息——它的类型是 [AnyObject]。要再次将其视为 图像数组,您需要一个演员表:

    let shuffledPicturePieces = GKRandomSource.sharedRandom()
        .arrayByShufflingObjectsInArray(picturePieces) as! [UIImage]
    

    你的第二期:

    使用未解析的标识符“shuffledPicturePieces”

    与范围有关。您在shuffle() 函数中定义了一个名为shuffledPicturePieces within 的数组,因此该名称仅在同一函数中的其他位置有效。在另一个函数中,例如您的 getPictureButton,该名称不存在。

    如果您希望shuffledPicturePieces 全局存在(或在您的类的实例中),请将其设为属性。例如:

    class Whatever {
        let picturePieces = ["concordTL", /*...*/].map { /* as above */ }
        var shuffledPicturePieces: [UIImage]!
        func shuffle() {
            shuffledPicturePieces = GKRandomSource.sharedRandom()
                .arrayByShufflingObjectsInArray(picturePieces) as! [UIImage]
            // notice no var or let here — assign to the instance property,
            // don't create a new local variable
        }
        func getPictureButton(sender: UIButton) {
            // as before
        }
    }
    

    顺便说一句,如果您使用标准 Swift 命名约定,它有助于与他人交流、调试以及 Stack Overflow 的语法高亮显示:您的 @IBOutlet 变量名称应以小写字母开头(easyTopLeft 等),因此它们不会被误认为是类型。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-11-06
      • 1970-01-01
      • 1970-01-01
      • 2014-06-30
      • 1970-01-01
      • 2012-07-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多