How to write a method to sort out black and red cards Newbie to Swift

import Cocoa import Darwin // STRUCTURES & ENUMS page 19/525 // DATE CHANGES //*********************************************************** // 17Jan2023 We have 2 enumns and one structure here // 1st enum rank //***********************************************************

enum Rank: Int { case Ace = 1 case Two,Three,Four,Five,Six,Seven,Eight,Nine,Ten case Jack,Queen,King func simpleDescription () -> String { switch self { case .Ace: return "ace" case .Jack: return " jack" case .Queen: return " queen" case .King: return " king" default: return String( self.rawValue) } } }// enum close

let ace = Rank.Ace let aceRawValue = ace.rawValue

let king = Rank.King let kingRawValue = king.rawValue print(kingRawValue) // king raw value should be 13 ok

// page 20 4 suits in deck of cards enum Suit { case Spades,Hearts, Diamonds,Clubs func simpleDescription()-> String { switch self { case .Spades: return "spades" case .Hearts: return "hearts" case .Diamonds: return "diamonds" case .Clubs: return "clubs" } } }// enum for suit close

let hearts = Suit.Hearts
let heartDescription = hearts.simpleDescription()
print(heartDescription)

// page 21/525 // Use structures to support enums struct Card {

var rank: Rank
var suit: Suit
var black1 = Suit.Spades
var black2 = Suit.Clubs
var red1 = Suit.Hearts
var red2 = Suit.Diamonds

func simpleDescription()->String{
 return"The rank is \(rank.simpleDescription()) of \(suit.simpleDescription())"
}

}// struc closingbrace

let threeOfSpades = Card(rank: .Three,suit:.Spades) let threeOfSpadesDescription = threeOfSpades.simpleDescription() print(threeOfSpadesDescription) // print the variable // EXPT develop a method to support for color : // red hearts and diamonds. // black spades and clubs

Welcome to the forum.

You should first format code properly, with code formatter button (</>|) below.

import Cocoa
import Darwin
// STRUCTURES & ENUMS page 19/525 // DATE CHANGES //***********************************************************
 // 17Jan2023 We have 2 enums and one structure here
 // 1st enum rank //***********************************************************

enum Rank: Int {
    case Ace = 1
    case Two,Three,Four,Five,Six,Seven,Eight,Nine,Ten
    case Jack,Queen,King
    
    func simpleDescription () -> String {
        switch self {
            case .Ace: return "ace"
            case .Jack: return " jack"
            case .Queen: return " queen"
            case .King: return " king"
            default: return String( self.rawValue)
        }
    }
}// enum close

let ace = Rank.Ace
let aceRawValue = ace.rawValue
let king = Rank.King
let kingRawValue = king.rawValue
print(kingRawValue) // king raw value should be 13 ok

// page 20 4 suits in deck of cards

enum Suit {
    case Spades, Hearts, Diamonds, Clubs
    func simpleDescription()-> String {
        switch self {
            case .Spades: return "spades"
            case .Hearts: return "hearts"
            case .Diamonds: return "diamonds"
            case .Clubs: return "clubs"
        }
    }
} // enum for suit close

let hearts = Suit.Hearts
let heartDescription = hearts.simpleDescription()
print(heartDescription)

// page 21/525 // Use structures to support enums
struct Card {
    var rank: Rank
    var suit: Suit
    var black1 = Suit.Spades
    var black2 = Suit.Clubs
    var red1 = Suit.Hearts
    var red2 = Suit.Diamonds
    
    func simpleDescription()->String{
        return"The rank is \(rank.simpleDescription()) of \(suit.simpleDescription())"
    }
} // struct closing brace

let threeOfSpades = Card(rank: .Three,suit:.Spades)
let threeOfSpadesDescription = threeOfSpades.simpleDescription()
print(threeOfSpadesDescription) // print the variable // EXPT develop a method to support for color : // red hearts and diamonds. // black spades and clubs

What do you want to do exactly ?

Where are all the cards stored (are they in an array ?) Please show.

Is it this array you want to sort ?

If so, if the array is

var allCards : [Card] = [ … all the cards …]

to get red is simply:

let redCards = allCards.filter() { card.suit == .Hearts || card.suit == .Diamonds }

to get black:

let blackCards = allCards.filter() { card.suit == .Spades || card.suit == .Clubs }

Note: I don't understand why you declare these properties in Card:

    var black1 = Suit.Spades
    var black2 = Suit.Clubs
    var red1 = Suit.Hearts
    var red2 = Suit.Diamonds

Thanks Claude31 for your help . I am going and type on computer all the book code from a book by Apple 525 pages long. I am on page 21. Its free online book

At this chapter just learning enum and structures. But this book ask for method to sort out black and red cards.

//  red  hearts and diamonds.
//  black spades and clubs
//  20 Jan 2023  ML
// a function called checkRed to determine red hearts and diamonds

func checkRed(RHearts:Bool, RDiamonds:Bool)-> Bool {
    let redVal: Bool
    // redVal is a return value in boolean format
    redVal = true
    // if the parameters RHearts,RDiamonds are set to true means red cards
//    let RHearts =
  //  let  RDiamonds =
   // they are red hearts & red diamond cards selected

    // redVal is boolean
    //logic must be ANDED
    if (RHearts && RDiamonds)  {
    print ("Red cards are :",Suit.Diamonds,Suit.Hearts )
    }
    else {
        _ = false
        print("Black cards are :", Suit.Clubs, Suit.Spades )
    }
    return redVal
}// end of checkRed func brace

checkRed(RHearts: true, RDiamonds:true)

var Array = [ "Ace:1","Two:2","Three:3 ","Four:4","Five:5 ","Six:6","Seven:7","Eight:8","Nine:9","Ten:10", "Jack:11","Queen:12","King:13"]

let cardType = ["Spades","Hearts","Diamonds","Clubs"]

for cardType in cardType { for Array in Array { print("(Array) of (cardType) ")

}
print("-----------------")

}

How to write a method to sort out black and red cards Newbie to Swift
 
 
Q