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 trialSimen Anthonsen
1,097 PointsCustomize textfield
Ok, so in my project i have created a deck of cards just like this:
enum CardSuit: Int {
case Diamonds = 0, Clubs, Hearts, Spades
}
enum CardValue: Int {
case Two = 2, Three, Four, Five, Six, Seven, Eight, Nine, Ten, Jack, Queen, King, Ace
}
class Card {
var value: CardValue
var suit: CardSuit
init(value initialValue: CardValue, suit initialSuit: CardSuit) {
value = initialValue
suit = initialSuit
My objective is to get an UITexftield to be allowed to only contain name of the cards. The card that was written would be sent to an array. For example if twoOfHearts was written in the textfield the card twoOfHearts would be sent to an array. My "dream" code would be something like this: array.append(textfield.text)
The user can write anything he wants and still be able to push the button, but if he don´t write a card´s name, he will get a notification saying something like "Not a valid input". My button will be enabled as long as there som kind of input in the textfield.
If the user writes a valid card name and pushes the button, he will get a notification saying "You played (cards name)", and the card will be assigned to an array. I have sort of managed that by doing this: if textfield.text! == "TwoHearts" { array.append(twoOfHearts) }
But I am thinking there have to be a better and simpler way?
1 Answer
Jhoan Arango
14,575 PointsHi,
Perhaps you want to evaluate first your textField by what the user types, and if it does not match any of your criteria, then alert the user. A switch statement can do that job for you, or an if statement as well. You can place the names of all your cards in a separate array, then iterate through it with a for in loop, and switch it with a best response.
something like
let cards = ["Hearts", "King","Etc"]
let textField = textField.text
for card in cards {
switch card {
case card where card == textField:
print("You played \(card)")
// Or Append to an array
case card where card == textField:
print("You played \(card)")
// Or Append to an array
default:
// Alert Window with other instructions or actions
}
}
Or something of that nature.
Hope this idea helps you.
Good luck
Simen Anthonsen
1,097 PointsSimen Anthonsen
1,097 PointsHi, thanks for your help. Looks like a good idea, but when i try to append the card in an array like this:
I get an error saying "Cannot convert value of type "String" to expected argument type "Card"
The print statement works fine, but seeing as card is a string it can´t get appended to an array of Cards which is the whole point.
The card that gets appended to an array, needs to have the value and suit value.