美文网首页
Project5-UITextChecker

Project5-UITextChecker

作者: 终极解码者 | 来源:发表于2016-09-26 13:42 被阅读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。

    相关文章

      网友评论

          本文标题:Project5-UITextChecker

          本文链接:https://www.haomeiwen.com/subject/jsjqyttx.html