Spelling Checker Tutorial

The UITextChecker object can be used to spelling-check a string. In this tutorial a few words will be displayed inside a Table View. When the words are selected, they will be checked for spelling, the background color will change to green if the spelling is correct, otherwise the background color will change to red. This tutorial is made in Xcode 7.2 for iOS 9.

Open Xcode and create a new Single View Application. For product name, use IOS9SpellingCheckerTutorial and then fill out the Organization Name and Organization Identifier with your customary values. Enter Swift as Language and make sure only iPhone is selected in Devices.

Remove the View Controller from the Storyboard and drag a Navigation Controller to the empty canvas. When the initial View Controller is deleted there isn't a starting point defined. Select the Navigation Controller and go to the Attribute Inspector. In the View Controller Section  select the "Is Initial View Controller" checkbox.

Double-click on the Navigation Bar in The Table View Controller and set the title to "Choose the right spelling".  Select the Table View Cell and go to the Attributes Inspector. In the Table View Cell section set the Identifier  to "cell".

The Storyboard should look like this.

Since we have deleted the View Controller from the Storyboard we can also delete the ViewController.swift file. Add a new file to the project, select iOS->Source->Cocoa Touch Class. Name it TableViewController and make it a subclass of UITableViewController.

Go to the TableViewController.swift file and add the following property

let words = ["devalopment", "development","devellopment"]

This array of strings will be displayed inside the Table View. Next, alter the predefined delegate methods.

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
      return 1

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return words.count

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath)

        // Configure the cell...
        cell.textLabel?.text = words[indexPath.row]
        cell.textLabel?.backgroundColor = UIColor.clearColor()

        return cell

The Table View has one section and three rows from the words array. The background color of the cell will be set to a clear color, because later on the background color of the cell will be changed. When the user selects a row from the Table View the tableView(_:didSelectRowAtIndexPath:) delegate method will be called.

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    let cell = tableView.cellForRowAtIndexPath(indexPath)!
    if wordIsSpelledCorrect((cell.textLabel?.text)!) {
      cell.backgroundColor = UIColor.greenColor()
    } else {
      cell.backgroundColor = UIColor.redColor()

a Table View Cell is initialized containing the word from the words array. If this word is spelled correctly. the cell's background will be green, otherwise it will be red. The actual spelling checker will be done in the wordIsSpelledCorrect method. 

func wordIsSpelledCorrect(word: String) -> Bool {
    let checker = UITextChecker()
    let range = NSMakeRange(0, word.characters.count)
    let wordRange = checker.rangeOfMisspelledWordInString(word, range: range, startingAt: 0, wrap: false, language: "en")
    return wordRange.location == NSNotFound

The UITextChecker class can be used to check a string for the right spelling. a range is created to spelling-check the whole word. The rangeOfMisspelledWordInString method is used to check the range for errors. If a word is spelled correctly a NSNotFound is returned from this method so the return value will be a boolean with the value of true, otherwise the value will be false.

Build and Run the project, select the rows, if the word is spelled correctly the background color will change to green. otherwise it is misspelled and the background color will change to red.

You can download the source code of the IOS9SpellingCheckerTutorial at the ioscreator repository on Github.