I, indem Sie die folgenden eine Reihe von UIImages randomisieren versuchen:Swift 2.2: Mit GKRandomSource mit einer Reihe von 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)
HIER: Ich erhalte die Fehlermeldung „Kann nicht Wert vom Typ umwandeln‚[ UIImage?] 'Zum erwarteten Argumenttyp' [AnyObject] '"im (picturePieces).
Kann GKRandomSource nicht mit UIImage arbeiten?
Ich möchte dann jeder der randomisierten UIImages in shuffledPicturePieces jedem meiner @IBOutlets zuweisen, wie unten dargestellt:
//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!")
}
}
HIER: Ich halte den Fehler „Verwendung von ungelösten Kennung‚shuffledPicturePieces‘bekommen "für jeden Eintrag in der IF-Anweisung.
Vielen Dank für Ihre Hilfe!
-Frank