Skip to content

Latest commit

 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 

Day 30: Project 5: WordScramble (Part Two)

Follow along at https://www.hackingwithswift.com/100/swiftui/30.

📒 Field Notes

This day covers Part Two of Project 5: WordScramble in the 100 Days of SwiftUI Challenge. (Project 5 files can be found in the directory for Part One.)

It focuses on several specific topics:

  • Adding to a list of words
  • Running code when our app launches
  • Validating words with UITextChecker

Running code when our app launches

The onAppear modifier is your friend here.

Validating words with UITextChecker

Now we're getting our hands dirty. In order to check if a word is "valid English" (ideally, though, we'd localize the language), we can utilize UITextChecker -- which is available through UIKit... but which mainly serves as a bridge to Objective-C-based range operations:

var currentGuessIsRealWord: Bool {
    let wordRange = NSRange(location: 0, length: currentRootWord.utf16.count)

    let misspelledRange = Self.textChecker.rangeOfMisspelledWord(
        in: currentRootWord,
        range: wordRange,
        startingAt: 0,
        wrap: false,
        language: "en"
    )

    return misspelledRange.location == NSNotFound
}

It's all good, though. Clunky as it may be, it's pretty cool that this kind of magic is within reach through some of Apple's lower-level APIs.

📸 Screenshots