开发者

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. At index position 1, I have an object, which I think is a string. 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 x-x-x-x on my screen. 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 x-x-x-x on my screen, but just to have a blank instead. 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, i.e. even if the string at position 1 of my array is 'x-x-x-x', it will still not set my UISampleLabel to nothing.

I suppose that I am getting confused with the @"" markers. When do I really need them? Why can't I simply code tempDateStri开发者_如何学Pythonng = [NoteBook objectAtIndex:1]; without the formatting thing?

Any help and suggestions would be very much appreciated!


You need to compare string with 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]; without the formatting thing?

You can. Why do you think you can't?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜