100 Days of SwiftUI

Day 29

2026-05-17

Project 5, part 1

“the last easy project we have on this course”

Lists, strings, and Unicode oh my!

Word Scramble: Introduction

“Word Scramble” — find the anagrams in a word game

Introducing List, your best friend

  • while you many not use List the most, you will rely on it the most
  • List provides a scrollable table of data for presentation (unlike Form which allows for data entry) — Form is a specialized List
  • can mix static and dynamic rows in a list
  • can Section lists
  • can generate its rows entirely from dynamic content without needing a ForEach, e.g. List(0..<5) {Text("Dynamic row \($0)")}

Loading resources from your app bundle

  • bundles are used to hold everything about the app
  • it’s common to put “extra” files in your bundle that your app will need
  • find the file using it’s URL (i.e. Uniform Resource Locator)
  • need to use URL because the OS paths are impossible to guess
  • use Bundle.main.url() — if the file exists, it will be returned, otherwise it will return nil
  • once you have the file as a String you can do stuff with it
func testBundles() {
    if let fileURL = Bundle.main.url(forResource: "some-file", withExtension: "txt") {
        // we found the file in our bundle!
        if let fileContents = try? String(contentsOf: fileURL){
            // we loaded the file into a string!
        }
    }
}

Working with strings

  • can do a lot of things with strings
    • split into an array
    • remove whitespace
    • spellcheck
  • once the string is in an array (however you’ve split it) you can return a .randomElement() (which returns an optional string)
  • use .trimmingCharacters(in: .whitespacesAndNewlines) from the CharacterSet struct so remove all the whitespace
  • use the “slightly unwieldy” UITextChecker for spellcheck
  • Swift understands e.g. emoji (i.e. UTF16), but Objective-C doesn’t so this is the fiddly part
  • UITextChecker will return NSNotFound if there are no misspellings
func testStrings() {
    let word = "swift"
    
    // get an instance of the UITextChecker
    let checker = UITextChecker()
    
    // let UITextChecker know the encoding
    let range = NSRange(location: 0, length: word.utf16.count)
    
    // where was the misspelled word found
    let misspelledRange = checker.rangeOfMisspelledWord(in: word, range: range, startingAt: 0, wrap: false, language: "en")
    
    // if there wasn't a misspelled word, UITextChecker returns `NSNotFound`
    let allGood = misspelledRange.location == NSNotFound

}