将NSString与UITextField文本进行比较永远不会被调用

时间:2023-01-01 08:09:01

I record the value of the text in my UITextField and I want to compare the text to the original text field value later. I try something like this, but I never get the NSLog to be displayed. Any ideas why?

我在UITextField中记录文本的值,我想稍后将文本与原始文本字段值进行比较。我尝试这样的东西,但我从来没有得到NSLog显示。有什么想法吗?

defaultTopicText = topicTextField.text;
if ([topicTextField.text isEqualToString:defaultTopicText]){
    NSLog(@"YES");
}else{
    NSLog(topicTextField.text);
    NSLog(defaultTopicText);
}

The code looks exactly like you see it. The first line I assign the value and the other - I compare with it. And it's not being called.

代码看起来与您看到的完全一样。第一行我赋值,另一行 - 我与之比较。并没有被召唤。

EDIT:

编辑:

The code itself IS getting called and I also get the same values when I put them in NSLog. Might the problem be that the text field contains @"\n" characters?

代码本身被调用,当我将它们放入NSLog时,我也得到相同的值。可能的问题是文本字段包含@“\ n”字符?

NSLog gives me this:

NSLog给了我这个:

2013-03-18 20:45:22.037 myapp[524:907] 

Here comes the text
2013-03-18 20:45:22.039 myapp[524:907] 

Here comes the text

3 个解决方案

#1


1  

Try to print out the value of the topicTextField.text and see what is shows. otherwise set the breakpoints to see if you are reaching to that particular line of code.

尝试打印出topicTextField.text的值并查看显示的内容。否则设置断点以查看您是否到达该特定代码行。

You coud also try comparing after removing the white spaces and new line, if there might be any

你可以尝试比较删除空格和新行后的比较,如果可能有的话

NSString *trimmmedText = [topicTextField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

if ([trimmmedText isEqualToString:defaultTopicText]){
    NSLog(@"YES");
}

#2


0  

Try changing to this:

尝试更改为:

NSString *newString = [defaultTopicText stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

if ([newString isEqualToString:defaultTopicText]){
    NSLog(@"YES");
}

#3


0  

I typed the following the figured out the answer... running this should give you your answer:

我打字以下找出了答案......运行这个应该给你答案:

if(!defaultTopicText){
    NSLog(@"defaultTopicText is nil");
}else{
    NSLog(@"defaultTopicText is a: %@".[defaultTopicText classname]);
}

defaultTopicText = topicTextField.text;
if ([topicTextField.text localizedCaseInsensitiveCompare:defaultTopicText] == NSOrderedSame){
    NSLog(@"YES");
}else{
NSLog(@"\"%@\" != \"%@\"",defaultTopicText, topicTextField.text);
}

Then I realized: topicTextField.text can only not be the same object as itself using this comparison method if it is nil.

然后我意识到:如果它是nil,topicTextField.text只能使用这个比较方法与它自己不是同一个对象。

topicTextField.text has to be nil... so it ends up executing:

topicTextField.text必须是nil ...所以它最终执行:

id var = nil;
[var isEqual:nil];

and the runtime makes that return 0;

并且运行时使返回0;

... so fix your outlet to topicTextField

...所以修复你的出口到topicTextField

#1


1  

Try to print out the value of the topicTextField.text and see what is shows. otherwise set the breakpoints to see if you are reaching to that particular line of code.

尝试打印出topicTextField.text的值并查看显示的内容。否则设置断点以查看您是否到达该特定代码行。

You coud also try comparing after removing the white spaces and new line, if there might be any

你可以尝试比较删除空格和新行后的比较,如果可能有的话

NSString *trimmmedText = [topicTextField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

if ([trimmmedText isEqualToString:defaultTopicText]){
    NSLog(@"YES");
}

#2


0  

Try changing to this:

尝试更改为:

NSString *newString = [defaultTopicText stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

if ([newString isEqualToString:defaultTopicText]){
    NSLog(@"YES");
}

#3


0  

I typed the following the figured out the answer... running this should give you your answer:

我打字以下找出了答案......运行这个应该给你答案:

if(!defaultTopicText){
    NSLog(@"defaultTopicText is nil");
}else{
    NSLog(@"defaultTopicText is a: %@".[defaultTopicText classname]);
}

defaultTopicText = topicTextField.text;
if ([topicTextField.text localizedCaseInsensitiveCompare:defaultTopicText] == NSOrderedSame){
    NSLog(@"YES");
}else{
NSLog(@"\"%@\" != \"%@\"",defaultTopicText, topicTextField.text);
}

Then I realized: topicTextField.text can only not be the same object as itself using this comparison method if it is nil.

然后我意识到:如果它是nil,topicTextField.text只能使用这个比较方法与它自己不是同一个对象。

topicTextField.text has to be nil... so it ends up executing:

topicTextField.text必须是nil ...所以它最终执行:

id var = nil;
[var isEqual:nil];

and the runtime makes that return 0;

并且运行时使返回0;

... so fix your outlet to topicTextField

...所以修复你的出口到topicTextField