我试图通过执行以下操作随机化一组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)
HERE:我不断收到错误“无法将类型'[UIImage?]'的值转换为(picturePieces)中预期的参数类型'[AnyObject]'”。
GKRandomSource不能与UIImage一起使用吗?
然后我想将shuffledPicturePieces中的每个随机UIImages分配给我的每个@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'”。
感谢您的帮助!
-Frank
答案 0 :(得分:0)
您的第一个错误:
无法转换类型' [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'
与范围有关。您在 shuffledPicturePieces
函数中定义了一个名为shuffle()
的数组,因此该名称仅在该函数的其他位置有效。在另一个功能中,例如您的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
等)开头,因此他们不会被误认为类型。