有没有办法进行字符串检查,如果它是一个单词,然后定义它?

时间:2016-01-30 06:45:04

标签: iphone swift uikit

我一直在学习Swift编程语言并且遇到了障碍。我已经弄清楚如何通过使用NSString来检查一个单词是否真实,但是我想知道一旦单词被声明为真有一种方法来定义单词。这是声明它是否真实的代码。

func wordIsReal(word: String) -> Bool {
    let checker = UITextChecker()
    let range = NSMakeRange(0, word.characters.count)
    let misspelledRange = checker.rangeOfMisspelledWordInString(word, range: range, startingAt: 0, wrap: false, language: "en")

    return misspelledRange.location == NSNotFound
}

2 个答案:

答案 0 :(得分:1)

您可以使用public function buildForm(FormBuilderInterface $builder, array $options) { $builder ->add('name', 'entity', array( 'class' => 'AppBundle\Entity\Type', //'property' => 'name', is deprecated use 'choice_label' instead 'expanded' => true, 'choice_label' => 'name', 'choice_translation_domain' => 'entity', )); } 检查单词是否有定义并显示该定义。

答案 1 :(得分:1)

谢谢你,你给了我找到这个词的解决方案。我试过了。

let word = "word
if UIReferenceLibraryViewController.dictionaryHasDefinitionForTerm(word)
{
    let ref: UIReferenceLibraryViewController = UIReferenceLibraryViewController(term: word)
    self.presentViewController(ref, animated: true, completion: nil)
} 

使用TextChecker的另一种方法

func wordIsReal(word: String) -> Bool 
{
   let checker = UITextChecker()
   let range = NSMakeRange(0, word.characters.count)
   let misspelledRange = checker.rangeOfMisspelledWordInString(word, range: range, startingAt: 0, wrap: false, language: "en_US")
   NSLog("misspelledRange:\(misspelledRange)")
   NSLog("word:\(word)")
   let arrGuessed:NSArray? = checker.guessesForWordRange(misspelledRange, inString: word, language: "en_US")as NSArray!
   NSLog("arrGuessed:\(arrGuessed)")
   return misspelledRange.location == NSNotFound
}

请参阅以下答案和解决方案

Answer 1

Answer 2

Error Solution