I am just learning how to use Xcode and Swift and am having trouble on an initial project. I am making a card game that uses a deck of cards. I create the deck of cards and then shuffle them in the viewDidLoad() function:
override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view. var deck = [Card]() for i in 1...14 { for suit in suits { let c = Card() c.suit = suit c.rank = i deck.append(c) } } deck.shuffle() }
I want to use the deck object when a button is pressed, but when I reference deck in an IBAction function when a button is pressed, it cannot find deck in the scope
@IBAction func buttonOneTapped(_ sender: Any) { if round == 1 { print(deck) if ["H", "D"].contains(deck[deckPosition].suit) { round += 1 } showCard(c: card1, cardToShow: deck[deckPosition]) } }
I tried creating the deck with the for loop in the overall ViewController class but am getting an "Expected declaration" error, I'm assuming because I'm using a for loop outside of a method.
Where should I instantiate the deck so that I can reference it when various buttons are pressed?
Thanks for the help!
https://stackoverflow.com/questions/67444529/where-to-instantiate-variables-in-view-controller-xcode May 08, 2021 at 01:58PM
没有评论:
发表评论