randomElement() returns an Optional element, so it will need to be unwrapped at some point before it can be used.
So something like:
var card = deck.randomElement()
print("My card is \(card?.suit ?? "no card")")
By the way, this:
card = (String) (rndNum)
Is not the usual way to convert an Int to a String in Swift. Either of these would be better:
card = String(rndNum)
card = "\(rndNum)"
The code you have technically turns rndNum not into a String but into a (String), or a single-member tuple consisting of a String. Although those get treated by the language as the equivalent of the single element’s type, they aren’t actually the same thing, as you can see if you check the type on your card variable.
Because you are giving it an Optional value to print out.
This isn’t really a problem, unless you just hate seeing warnings in Xcode; warnings don’t prevent your code from running and usually won’t crash your app. If you keep it an Optional, the output will look something like:
I drew a nil of nil
I drew a Optional(9) of Optional("Hearts")
(depending on if the Optional is nil or not)
If, however, you supply a default value via nil coalescing:
print("I drew a \(card?.cnumb ?? 0) of \(card?.suit ?? "no suit")")
Okay. I just decided to look at the solution and I was wondering why it was so different than what I had written. Also when I typed up the solution it had decided to throw a couple of cannot find variable in scope for each of the functions. Deck, message, suits, and generatedLog are throwing scope errors in addCard and drawCard functions.
//
// ContentView.swift
// Module2Challenge2
//
// Created by Eric Beecroft on 10/13/21.
//
import SwiftUI
struct Card{
//Initialize card number and suit to defaults
var num = 1
var suit = 0
}
struct ContentView: View {
//Important state variables
@State var generatedLog = [String]()
@State var message = ""
//Sets up values for the cards
@State var deck = [Card]()
let suits = ["Clubs", "Spades", "Hearts", "Diamonds"]
//The main program
var body: some View {
VStack(spacing: 10.0){
Text(message)
HStack(spacing: 10.0){
Button("Add Card"){
addCard()
}
Button("Draw Card"){
}
}
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
func addCard(){
//Draws Random cards
let randNumber = Int.random(in: 1...13)
let randSuit = Int.random(in: 0...3)
let newCard = Card(num: randNumber, suit: randSuit)
let numberSuitString = String(newCard.num) + "/" + String(newCard.suit)
if generatedLog.contains(numberSuitString){
message = "Generated duplicate card."
}
else{
deck.append(newCard)
generatedLog.append(numberSuitString)
let cardName = getCardName(newCard.num)
let suitName = suits[newCard.suit]
message = "Generated a \(cardName) of \(suitName)"
}
}
func drawCard(){
if(deck.count == 0){
message = "No cards in the deck."
}
else{
let randIndex = Int.random(in: 0...deck.count-1)
let randomCard = deck[randIndex]
let cardName = getCardName(randomCard.number)
let suitName = suits[randomCard.suit]
messsage = "Drew a \(cardName) of \(suitName)"
}
}
func getCardName(_ cardNumber:Int) -> String{
if(cardNumber == 1){
return "Ace"
}
else if(cardNumber == 11){
return "Jack"
}
else if(cardNumber == 12){
return "Queen"
}
else if(cardNumber == 13){
return "King"
}
else{
return String(cardNumber)
}
}
Okay I just rearranged the functions to be inside the Content view structures and I am still getting the scoping errors. I thought moving the preview portion to be at the end should have fixed it as that is how it shows in the solution.
//
// ContentView.swift
// Module2Challenge2
//
// Created by Eric Beecroft on 10/13/21.
//
import SwiftUI
struct Card{
//Initialize card number and suit to defaults
var num = 1
var suit = 0
}
struct ContentView: View {
//Important state variables
@State var generatedLog = [String]()
@State var message = ""
//Sets up values for the cards
@State var deck = [Card]()
let suits = ["Clubs", "Spades", "Hearts", "Diamonds"]
//The main program
var body: some View {
VStack(spacing: 10.0){
Text(message)
HStack(spacing: 10.0){
Button("Add Card"){
addCard()
}
Button("Draw Card"){
}
}
}
}
}
func addCard(){
//Draws Random cards
let randNumber = Int.random(in: 1...13)
let randSuit = Int.random(in: 0...3)
let newCard = Card(num: randNumber, suit: randSuit)
let numberSuitString = String(newCard.num) + "/" + String(newCard.suit)
if generatedLog.contains(numberSuitString){
message = "Generated duplicate card."
}
else{
deck.append(newCard)
generatedLog.append(numberSuitString)
let cardName = getCardName(newCard.num)
let suitName = suits[newCard.suit]
message = "Generated a \(cardName) of \(suitName)"
}
}
func drawCard(){
if(deck.count == 0){
message = "No cards in the deck."
}
else{
let randIndex = Int.random(in: 0...deck.count-1)
let randomCard = deck[randIndex]
let cardName = getCardName(randomCard.number)
let suitName = suits[randomCard.suit]
messsage = "Drew a \(cardName) of \(suitName)"
}
}
func getCardName(_ cardNumber:Int) -> String{
if(cardNumber == 1){
return "Ace"
}
else if(cardNumber == 11){
return "Jack"
}
else if(cardNumber == 12){
return "Queen"
}
else if(cardNumber == 13){
return "King"
}
else{
return String(cardNumber)
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
You need to make your PlayingCard struct conform to the Equatable protocol.
Also, when posting code to these forums, place three backticks ``` on the line before your code and three backticks ``` on the line after your code so that it will be formatted properly. You can also highlight an entire code block and click the </> button on the toolbar to wrap the block for you.
This makes it far easier to read and also makes it easier for other posters to copy/paste the code in order to test solutions and such.