100 Days of SwiftUI Day 29

100 Days of SwiftUI – Day 29 – Word Scramble

It’s day 29 of the 100 Days of SwiftUI! Yesterday, we wrapped up our fourth project and completed challenges. Today, we’re diving into our fifth project, which is going to be an app called Word Scramble. Paul made it very clear that this will be the last “easy” project, so we better enjoy it while we can. Let’s dive in!

Word Scramble in SwiftUI: introduction

Our Word Scramble app will be another game. The game will show players a random eight-letter word, and ask them to make words out of it. For example, if the starter word is “development” they might spell “elope”, “pen”, “pole”, and so on.

Two fundamentals of programming in SwiftUI will feature heavily here. One we’re already familiar with: Strings. The other will be new to us: List.

Using List in SwiftUI

The List view type is the one we’ll rely on the most, according to Paul. This means that it will be a very important component of our apps.

The job of a List view is to provide a scrolling table of data. In that, it’s pretty much identical to Form, except it’s used for presentation of data rather than requesting user input. Let’s take a look at an example.

struct ContentView: View {
    
    let people = ["Finn", "Leia", "James", "Michael"]
    
    var body: some View {
        List {
            Section("Section 1") {
                Text("Static row 1")
                Text("Static row 2")
                Text("Static row 3")
            }
            Section("Section 2") {
                ForEach(0..<5) {
                    Text("Dynamic row \($0)")
                }
            }
            
            Section("Section 3") {
                ForEach(people, id: \.self) {
                    Text("\($0)")
                }
            }
            
            Section("Section 4") {
                Text("Static row 4")
            }
        }
    }
}

As you’ll see in the example above, a List can be used to show both static as well as dynamic properties. This makes them very powerful and incredibly useful.

Working with Strings in SwiftUI

While we’ve already used Strings quite extensively in our apps, there is a lot of advanced stuff we have not yet seen. There are powerful API’s that allow us to work with Strings in various ways, like removing characters or whitespace, splitting them into an array or even check spelling.

Swift gives us a method called components(separatedBy:) that can converts a single String into an array of strings by breaking it up wherever another String is found. For example, this will create the array ["a", "b", "c"].

let input = "a b c"
let letters = input.components(separatedBy: " ")

The following example does the same thing, however, instead of separating the String each time a whitespace is found, this one separates them each time there’s a linebreak.

let input = """
            a
            b
            c
            """
let letters = input.components(separatedBy: "\n")

Paul also gave us a quick look at setting up a spellchecker, which uses Apple’s older framework UIKit. We have not properly implemented it yet, though I thought it would be fun to share anyway.


    func spellChecker() {
        let word = "swift" // our String to be checked
        let checker = UITextChecker() // initialize the spelling checker
        
        let range = NSRange(location: 0, length: word.utf16.count) // NSRange details what part of the String needs to be checked. In this case we say all of it, by saying location 0 (start at the first character) and the length being the length of our word property.
        let misspelledRange = checker.rangeOfMisspelledWord(in: word, range: range, startingAt: 0, wrap: false, language: "en")
        
        let allGood = misspelledRange.location == NSNotFound // if no spelling errors where found, it will return nill.

    }

And that’s it for day 29! Tomorrow, we’ll be diving into the Word Scramble app and start to implement List as well as various advanced String functions. Last but not least, we’ll also read a .txt file from our AppBundle. Something I did not cover today, but will do tomorrow. I’m very much looking forward to this app, so it’s time to recharge and get ready!

Darryl

Hi! My name is Darryl and this is my personal blog where I write about my journey as I learn programming! You'll also find articles about other things that interest me including games, tech and anime.

Post navigation

Leave a Comment

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

100 Days of SwiftUI – Day 63

100 Days of SwiftUI – Day 83

100 Days of SwiftUI – Day 38

100 Days of SwiftUI – Day 65