找不到类型' UIImage'的初始值设定项。接受类型'(命名:UIImage?)'的参数列表?

时间:2016-06-29 12:30:50

标签: ios iphone swift

尝试将此元组添加到数组时出现上述错误。

struct Cards{
    var cards: [(face: String, value: Int, image: UIImage?)] = []
}

var playerCards: [(face: String, value: Int, image: UIImage?)] = []

func dealCards() {
    self.createCards()
    var turn = 0
    for var i = 0; i <= self.cards.cards.count - 1; ++i {
        if turn == 0 {
            turn = 1
            self.playerCards.append((face: self.cards.cards[i].face, value: self.cards.cards[i].value, image: self.cards.cards[i].image))
        } 
        else {
            turn = 0
        }
    }
}

func createCards(){
    cards.cards.append(face: "Ace Of Spades", value: 14, image: UIImage(named: "ace_of_spades.png"))
}

1 个答案:

答案 0 :(得分:0)

为什么要按UIImage对象初始化UIImage个对象?也许这是对的?

func dealCards() {
    self.createCards()
    var turn = 0
    for var i = 0; i <= self.cards.cards.count - 1; ++i {
        if turn == 0 {
            turn = 1
            self.playerCards.append((face: self.cards.cards[i].face, value: self.cards.cards[i].value, image: self.cards.cards[i].image))
        } else {
            turn = 0
        }
    }
}

(我还修复了从0i的图像索引)

更新。修补了添加的代码:

func createCards() {
    cards.cards.append((face: "Ace Of Spades", value: 14, image: UIImage(named: "ace_of_spades.png")))
}