Skip to content

Instantly share code, notes, and snippets.

@robtimp
Last active May 4, 2018 23:06
Show Gist options
  • Save robtimp/c326fd7272bb2ab9976fff4d70db2afd to your computer and use it in GitHub Desktop.
Save robtimp/c326fd7272bb2ab9976fff4d70db2afd to your computer and use it in GitHub Desktop.
Updated for Swift 4
struct NoteSpeller {
static let wordURL = "https://raw.githubusercontent.com/dwyl/english-words/master/words.txt"
static let delimiter: Character = "\n"
static let notesNames: [Character] = ["a", "b", "c", "d", "e", "f", "g"]
static func getWords(includeH: Bool = false) -> [String] {
var notes = notesNames
if includeH {
notes.append("h")
}
guard let url = URL(string: wordURL),
let file = try? String(contentsOf: url) else {
return []
}
let words = file.split(separator: delimiter).map { String($0) }
let result = words.filter { word in
for character in word {
if !notes.contains(character) {
return false
}
}
return true
}
return result
}
}
NoteSpeller.getWords()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment