Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialOscar Armenta
641 PointsMaking my own app
Hi community, I know this is not completely related but. I am trying to make my own app, is about a concentration game. Given a bunch of cards facing down, you have to find the pairs. But for some reason my app doesn't show the rest of the emojis (I decided to put emojis instead of images) I think the mistake is in the view controller, inside the touchCardHelper function. If you see it you will understand. However I have try many things so is changing the instance of the structure, not making more copies of the structure, cuze that's what it seems to be doing. I have not idea whatsoever of what is going on. Please help me :(
P.D. I have already wrote the rest of the code within the touchCardHelper function so the game does more things, however I just put this here because not even the basic actions are working :(
Modeling the card structure code:
public struct Card { var identifier = 0
var isFaceUp = false
var isMatched = false
var isRecognizable = 0
var emoji = "😈"
var hasEmoji = false
}
Modeling the game class structure:
public class Game { var cards = Card
var points = 3
let emojis = ["👽", "🥳", "😈", "👾"]
init(PairOfCards: Int) {
for _ in 1...PairOfCards {
var card = Card()
card.identifier += 1
for unicode in emojis {
if card.hasEmoji == false {
card.emoji = unicode
card.hasEmoji = true
}
}
cards.append(card)
cards.append(card)
cards.shuffle()
}
}
}
Modeling the view controller:
var kakaoGame = Game(PairOfCards: 2)
@IBAction func touchCard(_ sender: UIButton) {
touchCardHelper(sender)
}
func cardChanger (cardInstance: inout Card) -> Void
{
cardInstance.isFaceUp = true
}
func touchCardHelper(_ button: UIButton) -> Void {
print("Hey I am working")
for var eachCard in kakaoGame.cards {
print("entering the for loop")
if button.backgroundColor == #colorLiteral(red: 0, green: 0, blue: 0, alpha: 1) && eachCard.isFaceUp == false {
print("entering the if statement")
button.setTitle(eachCard.emoji, for: UIControl.State.normal)
cardChanger(cardInstance: &eachCard)
print("\(eachCard.isFaceUp)")
button.backgroundColor = #colorLiteral(red: 0.9999960065, green: 1, blue: 1, alpha: 1)
}
}
}
1 Answer
Oscar Armenta
641 PointsI think the problem is the fact that apparently given the pointer that represents each element within each iteration in a for loop CAN'T change the properties nor the element itself. Do somebody can confirm this please?