Train of thought

Create UI structures using SwiftUI;

The data is generated using Swift’s enumerations and structures, and the data is consolidated through the viewModel for presentation (the interaction is not done yet, so there is no data binding involved in the MVVM design pattern).

rendering

Canvas live preview of iPhone renderings:

Running iphone11 dark mode

Running the iPad Air simulator

Related code parsing

Enumeration creates card numbers

//CaseIterable: When traversing an enumeration, follow the CaseIterable protocol and iterate through the allCases class attribute of the enumeration
enum Rank:Int.CaseIterable {
    case ace = 1
    
    The default value is "in order" plus 1, so rawValue for two is 2, rawValue for three is 3, and so on
    case two,three,four,five,six,seven,eight,nine,ten
    case J.Q.K
    
    // Enumeration of swift, you can customize methods. When writing code, "So fast!"
    func customDescription(a)->String{
        switch self {
        case .ace:
            return "A"
        case .J:
            return "J"
        case .Q:
            return "Q"
        case .K:
            return "K"
        default:
            return "\ [self.rawValue)"}}}Copy the code

Enumeration creates card types

enum CardType: CaseIterable{
    case heart,spades,club,diamond
    
    func customDescription(a) -> String {
        switch self {
        case .heart:
            
            // Command + CTRL +space to quickly bring up the emoji window, you can search, "Very 6 ah!"
            return "Has ️"
        case .spades:
            return "♠ ️"
        case .club:
            return "♦ ️"
        default:
            return "♣ ️"}}}Copy the code

The viewModel logic

struct GameVM {
    
    /// declare it private, which is in line with the idea of encapsulation. When initializing a method, it must be a class method (static func)! If the instance method is used, the instance is created when the attribute is not initialized, which is not syntactic!
    private var model:GameM = generateGame()
    
    // Array generics cannot be declared as Card directly. Need to click through the structure name!
    var cards: Array<GameM.Card> {
        return model.cards
    }
     
    static func generateGame(a)->GameM{
        var lArr: Array<GameM.Card> = Array<GameM.Card> ()// Two loops to create the data source, "soon!"
        for type in CardType.allCases {
            for rank in Rank.allCases{
                lArr.append(GameM.Card(rank: rank, type:type))
            }
        }
        return GameM(cards: lArr)
    }
}
Copy the code

The UI implementation

/// declarative UI programming, "soon!"
struct ContentView: View {
    var viewModel: GameVM
    
    var body: some View {
        let columnNum = 4
        let rowNum = viewModel.cards.count/columnNum
        
        return
            HStack{
                ForEach(0..<columnNum){column in
                    VStack{
                        ForEach(0..<rowNum){row in
                            let index = (rowNum) * column + row
                            let card = viewModel.cards[index]
                            Card(cardM:card)
                        }
                    }
                }
            }.padding()
    }
}
Copy the code

The source code

FullDeckOfCards_SwiftUI

feeling

Swift syntax is much easier to build data types than OC, and enumerations and structs (value types, copy-on-write) are powerful.

Structs are preferred for unshared data, and classes are typically used only for viewModels, where data is shared among multiple Views.

SwiftUI uses a declarative approach to build the UI, which is much simpler in terms of code, one set of code, three ends (iOS, iPadOS, macOS (M1)). And support real-time preview, greatly improve the EFFICIENCY of UI development!

IOS13, the lowest version of APP support should not be far away (wechat currently supports the lowest iOS11.0), iOSer, it is time to learn a wave of SwiftUI! Feel free to brush up on swift grammar again.

Looking ahead, iosers will finally be able to shout, “Soon!”

Amuse oneself:

Just a friend asked me, “OC teacher “what happened, I said how to return a responsibility, gave me a few screenshots, I looked, ao, the original is yesterday, there are two young people, to achieve the demand, a use of more than 90 minutes, a use of more than 80 minutes……

But it doesn’t matter. After more than 200 minutes, the requirements are ready. I said Kotlin, you don’t speak martial arts, you don’t understand, he said “OC teacher “sorry, I don’t understand, I randomly hit, then he said he had practiced Java for three or four years, it seems to be bear from, this young man does not speak moral, come, cheat, come, sneak attack, I twenty-nine years old comrade, is that good? This is not good, I advise, the young man to look out for themselves, reflect, later do not make such mistakes, clever ah, development to harmony, to speak of hair, do not engage in a fight, thank you friends.