2017-04-20 59 views
0

這可能有一個簡單的答案,但我不能找到我已經做了什麼,所以我只是要發表評論的整個情況。這是我使用卡組的多維數組製作的紙牌遊戲的主要文件。我的變量被聲明,但我不能在迅速使用

class GameViewController: UIViewController 
{ 

var royal = false 
var HandMe: [[String]] = [[" ", " ", " ", " ", " "]] 
var turn = "Computer" 
var turns = 1 
var Pot: [[String]] = [[" ", " ", " ", " ", " "]] 
var Cards = [ 
    ["Queen", "Spades", "2", "yes", "Queen_Spades.jpg"], 
    ["King", "Spades", "3", "yes", "King_Spades.jpg"], 
    ["Jack", "Spades", "1", "yes", "Jack_Spades.jpg"], 
    ["Ace", "Spades", "4", "yes", "Ace_Spades.jpg"], 
    ["2", "Spades", "1", "no", "2_Spades.jpg"], 
    ["3", "Spades", "1", "no", "3_Spades.jpg"], 
    ["4", "Spades", "1", "no", "4_Spades.jpg"], 
    ["5", "Spades", "1", "no", "5_Spades.jpg"], 
    ["6", "Spades", "1", "no", "6_Spades.jpg"], 
    ["7", "Spades", "1", "no", "7_Spades.jpg"], 
    ["8", "Spades", "1", "no", "8_Spades.jpg"], 
    ["9", "Spades", "1", "no", "9_Spades.jpg"], 
    ["10", "Spades", "1", "no", "10_Spades.jpg"], 
    ["Queen", "Hearts", "2", "yes", "Queen_Hearts.jpg"], 
    ["King", "Hearts", "3", "yes", "King_Hearts.jpg"], 
    ["Jack", "Hearts", "1", "yes", "Jack_Hearts.jpg"], 
    ["Ace", "Hearts", "4", "yes", "Ace_Hearts.jpg"], 
    ["2", "Hearts", "1", "no", "2_Hearts.jpg"], 
    ["3", "Hearts", "1", "no", "3_Hearts.jpg"], 
    ["4", "Hearts", "1", "no", "4_Hearts.jpg"], 
    ["5", "Hearts", "1", "no", "5_Hearts.jpg"], 
    ["6", "Hearts", "1", "no", "6_Hearts.jpg"], 
    ["7", "Hearts", "1", "no", "7_Hearts.jpg"], 
    ["8", "Hearts", "1", "no", "8_Hearts.jpg"], 
    ["9", "Hearts", "1", "no", "9_Hearts.jpg"], 
    ["10", "Hearts", "1", "no", "10_Hearts.jpg"], 
    ["Queen", "Diamonds", "2", "yes", "Queen_Diamonds.jpg"], 
    ["King", "Diamonds", "3", "yes", "King_Diamonds.jpg"], 
    ["Jack", "Diamonds", "1", "yes", "Jack_Diamonds.jpg"], 
    ["Ace", "Diamonds", "4", "yes", "Ace_Diamonds.jpg"], 
    ["2", "Diamonds", "1", "no", "2_Diamonds.jpg"], 
    ["3", "Diamonds", "1", "no", "3_Diamonds.jpg"], 
    ["4", "Diamonds", "1", "no", "4_Diamonds.jpg"], 
    ["5", "Diamonds", "1", "no", "5_Diamonds.jpg"], 
    ["6", "Diamonds", "1", "no", "6_Diamonds.jpg"], 
    ["7", "Diamonds", "1", "no", "7_Diamonds.jpg"], 
    ["8", "Diamonds", "1", "no", "8_Diamonds.jpg"], 
    ["9", "Diamonds", "1", "no", "9_Diamonds.jpg"], 
    ["10", "Diamonds", "1", "no", "10_Diamonds.jpg"], 
    ["Queen", "Clubs", "2", "yes", "Queen_Clubs.jpg"], 
    ["King", "Clubs", "3", "yes", "King_Clubs.jpg"], 
    ["Jack", "Clubs", "1", "yes", "Jack_Clubs.jpg"], 
    ["Ace", "Clubs", "4", "yes", "Ace_Clubs.jpg"], 
    ["2", "Clubs", "1", "no", "2_Clubs.jpg"], 
    ["3", "Clubs", "1", "no", "3_Clubs.jpg"], 
    ["4", "Clubs", "1", "no", "4_Clubs.jpg"], 
    ["5", "Clubs", "1", "no", "5_Clubs.jpg"], 
    ["6", "Clubs", "1", "no", "6_Clubs.jpg"], 
    ["7", "Clubs", "1", "no", "7_Clubs.jpg"], 
    ["8", "Clubs", "1", "no", "8_Clubs.jpg"], 
    ["9", "Clubs", "1", "no", "9_Clubs.jpg"], 
    ["10", "Clubs", "1", "no", "10_Clubs.jpg"] 
    ] 
    var last: UInt32 = UInt32(Cards.endIndex - 1)//instance member 'Cards' cannot be used on type 'GameViewController' 
    var random = Int(arc4random_uniform(last) + 1)//instance member 'last' cannot be used on type 'GameViewController' 
    var TopCard = UIButton(type: .Custom) 
    let image = UIImage(named: Pot[Int(last)][4])//instance member 'Pot' cannot be used on type 'GameViewController' 
    TopCard.setImage(image, forState: .Normal) 
+1

使用屬性的代碼必須位於方法 – dan

+1

或計算屬性中。 – JAL

+0

@JAL不是基本上是一種計算屬性的方法嗎? – Honey

回答

0

使用先前定義的屬性的代碼必須位於方法內或初始化器內或計算屬性內。

class GameViewController: UIViewController { 
    var royal = false 
    var HandMe: [[String]] = [[" ", " ", " ", " ", " "]] 
    var turn = "Computer" 
    var turns = 1 
    var Pot: [[String]] = [[" ", " ", " ", " ", " "]] 
    var Cards = //huge array 
    var last: UInt32 
    var random: Int 
    var TopCard = UIButton(type: .Custom) 
    let image: UIImage 

    init() { 
    last = UInt32(Cards.endIndex - 1) 
    random = Int(arc4random_uniform(last) + 1) 
    image = UIImage(named: Pot[Int(last)][4]) 
    TopCard.setImage(image, forState: .Normal) 
    } 
} 
+0

在'init'方法中做得更好還是隻做一個計算屬性? – Honey

+0

在這種情況下,由於正在設置對象,最好使用'init'。 –

0

原來我宣佈這一切混亂的,甚至裝形式之前。這就是爲什麼我有一百萬個錯誤!感謝您的答案!

相關問題