使用UITextChecker时遇到问题 - Xcode / iPhone [英] Having trouble with UITextChecker - Xcode/iPhone

查看:108
本文介绍了使用UITextChecker时遇到问题 - Xcode / iPhone的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试使用UITextChecker来查看字符串中的单词实际上是否实际上是一个单词。我已经使用我在网上找到的示例将代码拼凑在一起,但我并不理解它,它有点高于我的水平。任何人都可以帮我弄清楚如何检查字符串中的单词是否真的是一个单词?我还在学习所有这些,所以非常感谢!

I've been trying to use UITextChecker to see if a word inside a string is, in fact, actually a word. I've pieced the code together using examples I've found online, but I don't really understand it, and it's a bit above my level. Can anyone help me figure out how to check if a word in a string is really a word? I'm still learning all of this so thanks a ton!

-(IBAction)pushButton:(id)sender{
label.text = textField.text;
NSString * currentWord;
currentWord = label.text;
NSRange range = NSMakeRange(0, 0);
range = [textChecker rangeOfMisspelledWordInString:[currentWord lowercaseString] 
                                             range:NSMakeRange(0, [currentWord length]) 
                                        startingAt:0 
                                              wrap:NO 
                                          language:@"en_US"];
if (range.location == NSNotFound) {
    NSLog(@"Word found");
}
    else {
        NSLog(@"Word not found");
    }

}

更新:我还有麻烦的是,因为我的日志显示Word not found,无论我放在文本字段中。我想发布我的.h文件以查看是否有错误。这里还有一个指向我收到原始代码的链接,如果有帮助的话。再次感谢。

UPDATE: I'm still having trouble with this, as my log is showing "Word not found" no matter what I put in the textfield. I wanted to post my .h file as well to see if something is incorrect. Also here is a link to where I received the original code, in case that helps. Thanks again.

 #import <UIKit/UIKit.h>
#import <UIKit/UITextChecker.h>

@interface CheckIfWordViewController : UIViewController {
    IBOutlet UILabel * label;
    IBOutlet UITextField * textField;
    IBOutlet UIButton * button;
    UITextChecker * textChecker;
}
@property (retain, nonatomic) UITextChecker * textChecker;
@property (retain, nonatomic) UIButton * button;
@property (retain, nonatomic) UILabel * label;
@property (retain, nonatomic) UITextField * textField;


-(IBAction)pushButton:(id)sender;

@end


推荐答案

我被UITextChecker抓住了,即使是拼写错误的单词也总是返回NSNotFound,因为我只给它CAP喂单词。通过提交[currentWord lowercaseString]解决了新手的一个小问题。我还从研究中发现,超过25个字符的单词也将返回NSNotFound而不管拼写是否正确。

I got caught out by UITextChecker always returning NSNotFound even for misspelled words because I was feeding it CAPs only words. A slight problem for rookies solved by submitting [currentWord lowercaseString]. I also discovered from researching this that words over 25 characters will also return NSNotFound irrespective of incorrect spelling.

这篇关于使用UITextChecker时遇到问题 - Xcode / iPhone的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆