简体   繁体   English

Objective-C:比较普通字符串和NSMutableArrays中找到的字符串

[英]Objective-C: Comparing normal strings and strings found in NSMutableArrays

I am confused about strings (a beginner's problem, I'm afraid): 我对字符串感到困惑(恐怕是初学者的问题):

I have one NSMutableArray called Notebook. 我有一个称为Notebook的NSMutableArray。 At index position 1, I have an object, which I think is a string. 在索引位置1,我有一个对象,我认为它是一个字符串。 At least I put it into the array like this: 至少我把它像这样放入数组:

[NoteBook replaceObjectAtIndex:1 withObject:@"x-x-x-x"];

So far so good. 到现在为止还挺好。 If I put this into an UILabel, it will show xxxx on my screen. 如果将其放入UILabel,它将在屏幕上显示xxxx。 The nightmare starts when I try to compare this string with other strings. 当我尝试将此字符串与其他字符串进行比较时,噩梦开始了。 Let's consider that I do not want to display the string xxxx on my screen, but just to have a blank instead. 让我们考虑一下,我不想在屏幕上显示字符串xxxx,而只想显示一个空白。 So I thought I could achieve this by coding this: 所以我想我可以通过编写以下代码来实现:

    NSString *tempDateString;
tempDateString = [NSString stringWithFormat:@"%@",[NoteBook objectAtIndex:1]];

if (tempDateString == @"x-x-x-x") {

        UISampleLabel.text = @"";

    }

For some reason, this does not work, ie even if the string at position 1 of my array is 'xxx-x', it will still not set my UISampleLabel to nothing. 由于某些原因,这是行不通的,即,即使我数组位置1处的字符串是“ xxx-x”,它也不会将UISampleLabel设置为空。

I suppose that I am getting confused with the @"" markers. 我想我对@“”标记感到困惑。 When do I really need them? 我何时真正需要它们? Why can't I simply code tempDateString = [NoteBook objectAtIndex:1]; 为什么我不能简单地编写tempDateString = [NoteBook objectAtIndex:1]; without the formatting thing? 没有格式化的东西?

Any help and suggestions would be very much appreciated! 任何帮助和建议将不胜感激!

You need to compare string with isEqualToString : 您需要将字符串与isEqualToString进行比较:

if ([tempDateString isEqualToString:@"x-x-x-x"]) {
  UISampleLabel.text = @"";
}

In addition to the question that's been answered: 除了已回答的问题:

Why can't I simply code tempDateString = [NoteBook objectAtIndex:1]; 为什么我不能简单地编写tempDateString = [NoteBook objectAtIndex:1]; without the formatting thing? 没有格式化的东西?

You can. 您可以。 Why do you think you can't? 为什么您认为自己做不到?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM