數組我試圖通過以下操作進行隨機UIImages數組:雨燕2.2:使用GKRandomSource與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每個我@IBOutlets的隨機UIImages的,如下圖所示:
//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!")
}
}
這裏:我不斷收到錯誤「使用未解決的標識符‘shuffledPicturePieces’ 「對於IF語句中的每個項目。
謝謝你的幫助!
-Frank