Advertisement
applehelpwriter

Swift: function that returnss another function as its value

Jun 4th, 2014
479
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //swift
  2.  
  3. //demonstrating a function that returns another function as its value
  4.  
  5. func addALetter() -> (String -> String) {
  6.     func addChars(char: String) -> String {
  7.         var twoChars = ""
  8.         var newChar = ""
  9.         if char == "z" {
  10.             newChar = "zy"
  11.         } else {
  12.         let alphabet = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y", "z"]
  13.         let count = alphabet.count
  14.         for i in 0..count {
  15.             if char == alphabet[i] {
  16.                 twoChars = alphabet[i+1]
  17.                 newChar = char + twoChars
  18.             }
  19.         }
  20.  
  21.         }
  22.  
  23.         return newChar
  24.     }
  25.     return addChars
  26. }
  27.  
  28. //let f = "g"
  29. var s = addALetter()
  30. s("c")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement