I decided to start working on Module 2 Challenge 2. Below is the code I got so far. Currently the buttons are generating error messages about conversions and missing argument format in the content view. I don’t know how to set the card and randomly pull cards. Any advice?
//
// ContentView.swift
// Shared
//
// Created by Eric Beecroft on 10/4/21.
//
import SwiftUI
struct Card{
var cnumb:Int
var suit:String
}
struct ContentView: View {
@State var deck:Card
var body: some View {
HStack{
Button('"One"')
Button('"Two"')
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
Not exactly sure since you didn’t specify what the errors you’re now getting are, but it’s probably because ContentView() in your preview needs to take a parameter. You declare @State var deck: Card but never initialize it within ContentView. Normally, that means you would pass in a value from a parent View or whatever, but this being an @State variable means you shouldn’t do that; typically a passed in state value would be @Binding instead of @State.
For me I know how to initialize something that is an int, but I don’t know how to initialize something that is a Card or a Deck. That is kind of where I am having difficulty.
So this is the error message I am getting from the random number generator. ‘RandomNumberGenerator’ cannot be constructed because it has no accessible initializers. I am making a bit of progress at least.
//
// ContentView.swift
// Shared
//
// Created by Eric Beecroft on 10/4/21.
//
import SwiftUI
struct Card{
var cnumb:Int
var suit:String
}
struct ContentView: View {
var suit = ["Spades", "Clubs", "Hearts", "Diamonds"]
@State var deck = [Card]()
var body: some View {
HStack{
Button("One"){
deck[0].cnumb = RandomNumberGenerator(1...13)
deck[0].suit = RandomNumberGenerator(0...suit.count - 1)
}
Button("Two"){
}
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
The RandomNumberGenerator is a protocol and should not be used in this manner
The correct method to generate a random Integer is:
let randomNumber = Int.random(in: 1...13)
For your random suit selection you would do something like this:
let index = Int.random(in: 0..<suit.count) let randomSuit = suit[index]
OR you can use this method which is available on arrays
let randomSuit = suit.randomElement()
Having created a randomNumber and a randomSuit you now need to create a new card with those values assigned to the new card properties and append that to the deck making sure that it is not a duplicate.
As you read through the challenge description you will notice that Chris Ching refers to a label that you use to advise the user what card has been generated (or if it is a duplicate) when you press the first button and what card has been drawn when you press the second button. So in your View you need to add a Text() object to give feedback to the user.
Huh I thought RandomNumber generator was the same as .random. Well I am progress at least little bit little. So now I am getting this error message: No exact matches in call to instance method ‘append’ when trying to append data.
//
// ContentView.swift
// Shared
//
// Created by Eric Beecroft on 10/4/21.
//
import SwiftUI
struct Card{
var cnumb:Int
var suit:String
}
struct ContentView: View {
var suit = ["Spades", "Clubs", "Hearts", "Diamonds"]
@State var deck = [Card]()
var body: some View {
HStack{
Button("One"){
let rndNum = Int.random(in: 1...13)
let rndSuit = suit[Int.random(in: 0..<suit.count)]
if(!indeck)
{
deck.append(cnumb: rndNum, suit: rndSuit)
print("Generated a \rndNum of \rndSuit")
}
else
{
print("Generated duplicate card!")
}
}
Button("Two"){
}
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
I am using indeck as a placeholder. If the card 8 is not in the deck of cards already then add it, otherwise print out that the card created is a duplicate card.
It is currently throwing the error message “Referencing instance method ‘contains’ on ‘Sequence’ requires that 'Card conform to ‘Equatable’”
I disabled the append command to cut down on error messages. The code looks similar to the C++ code, but a bit different. I didn’t know that contains actually existed on arrays.
//
// ContentView.swift
// Shared
//
// Created by Eric Beecroft on 10/4/21.
//
import SwiftUI
struct Card{
var cnumb:Int
var suit:String
}
struct ContentView: View {
var suit = ["Spades", "Clubs", "Hearts", "Diamonds"]
@State var deck = [Card]()
var body: some View {
HStack{
Button("One"){
let rndNum = Int.random(in: 1...13)
let rndSuit = suit[Int.random(in: 0..<suit.count)]
let myCard = Card(cnumb: rndNum, suit: rndSuit)
if(!deck.contains(myCard))
{
//deck.append(cnumb: rndNum, suit: rndSuit)
print("Generated a \rndNum of \rndSuit")
}
else
{
print("Generated duplicate card!")
}
}
Button("Two"){
}
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
The object that you append to deck must be of type Card which is the struct that you defined. Since you now have a myCard declared which you have used to test if it exists in the array deck, if the test is false you can append that card like this:
I just added myCard to the deck but the contains method appears to be not functioning. Its giving me the error message: Referencing instance method ‘contains’ on ‘Sequence’ requires that ‘Card’ conform to ‘Equatable’
Any Advice?
//
// ContentView.swift
// Shared
//
// Created by Eric Beecroft on 10/4/21.
//
import SwiftUI
struct Card{
var cnumb:Int
var suit:String
}
struct ContentView: View {
var suit = ["Spades", "Clubs", "Hearts", "Diamonds"]
@State var deck = [Card]()
var body: some View {
HStack{
Button("One"){
let rndNum = Int.random(in: 1...13)
let rndSuit = suit[Int.random(in: 0..<suit.count)]
let myCard = Card(cnumb: rndNum, suit: rndSuit)
if(!deck.contains(myCard))
{
deck.append(myCard)
print("Generated a \rndNum of \rndSuit")
}
else
{
print("Generated duplicate card!")
}
}
Button("Two"){
}
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
As the error message says, you need to make your Card struct conform to the Equatable protocol. Equatable is what lets you do == between two items.
To conform to Equatable, you would need to write a == function to tell the compiler how to test equality between two Cards.
HOWEVER, since Card's two properties are Int and String, which are already Equatable, you don’t need to do anything to make Card itself Equatable except this:
struct Card: Equatable {
var cnumb:Int
var suit:String
}
The compiler is able to synthesize Equatable conformance for you if all the properties are themselves Equatable once you tell it to do so.
Thanks Rooster. I was kind of confused why it was asking that. I also didn’t know there was a protocol for this already. Now the errors are gone but the deck doesn’t appear to build and I am not getting any print statements displaying in the console.
//
// ContentView.swift
// Shared
//
// Created by Eric Beecroft on 10/4/21.
//
import SwiftUI
struct Card: Equatable{
var cnumb:Int
var suit:String
}
struct ContentView: View {
var suit = ["Spades", "Clubs", "Hearts", "Diamonds"]
@State var deck = [Card]()
var body: some View {
HStack{
Button("Build Deck"){
let rndNum = Int.random(in: 1...13)
let rndSuit = suit[Int.random(in: 0..<suit.count)]
let myCard = Card(cnumb: rndNum, suit: rndSuit)
if(!deck.contains(myCard))
{
deck.append(myCard)
print("Generated a \rndNum of \rndSuit")
}
else
{
print("Generated duplicate card!")
}
}
Button("Two"){
}
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}