Project5-UITextChecker
2016-09-26 本文已影响0人
终极解码者
1.从文件中读取内容
if let startWordsPath = Bundle.main.path(forResource: "start", ofType: "txt") {
if let startWords = try? String(contentsOfFile: startWordsPath) {
allWords = startWords.components(separatedBy: "\n")
}
2.UITextChecker()类可以用来检查拼写的错误
func isReal(word: String) -> Bool {
let checker = UITextChecker()
let range = NSMakeRange(0, word.utf16.count)
//返回第一处拼写错误的range
let misspelledRange = checker.rangeOfMisspelledWord(in: word, range: range, startingAt: 0, wrap: false, language: "en")
return misspelledRange.location == NSNotFound
}
3.unowned 和 weak
let submitAction = UIAlertAction(title: "Submit", style: .default) { [unowned self, ac] action in
let answer = ac.textFields![0]
self.submit(answer: answer.text!)
}
两者都是为了避免循环引用,区别是weak允许值为nil,而unowned不允许值为nil。