Green - example 2

Open ViewController.swift, and replace the implementation of makeHeadline(from:) with the following lines of code:

func makeHeadline(from string: String) -> String { 
  let words = string.components(separatedBy: " ") 
   

  var headline = "" 
  for var word in words { 
    let firstCharacter = word.remove(at: word.startIndex) 
    headline += "(String(firstCharacter).uppercased())(word) " 
  } 
   
  headline.remove(at: headline.index(before: headline.endIndex)) 
  return headline 
} 

Let's go through this implementation step by step:

  1. Split the string into words.
  2. Iterate over the words, and remove the first character and change it to uppercase. Add the changed character to the beginning of the word. Add this word with a trailing space to the headline string.
  3. Remove the last space and return the string.

Run the tests. All the tests pass. The next thing to perform in the TDD workflow is refactoring.

Do not skip refactoring. This step is as important as the red and the green step. You are not done until there is nothing to refactor anymore.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.26.90