2016-11-29 46 views
0

数组我试图通过以下操作进行随机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

回答

0

你的第一个错误:

Cannot convert value of type '[UIImage?]' to expected argument type '[AnyObject]'

是因为要传递可选类型的数组给需要非可选类型的数组功能。既然你“知道”这些图像出现在您的资产目录,一个简单的方法,使他们不可选的是强制解开他们(注意添加!):

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] 

你的第二个问题:

Use of unresolved identifier 'shuffledPicturePieces'

与范围有关。您在shuffle()函数中定义了一个名为shuffledPicturePieces的数组,因此该名称仅在该函数中的其他位置有效。在另一个功能中,如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 
    } 
} 

顺便说一句,它可以帮助他人,调试,以及像堆栈溢出的语法高亮显示,如果你使用标准雨燕命名约定的事情通讯:您@IBOutlet变量的名称应该以一个较低的开始案件信(easyTopLeft等),所以他们不会误认为类型。