n.evermind
n.evermind

Reputation: 12004

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 tempDateString = [NoteBook objectAtIndex:1]; without the formatting thing?

Any help and suggestions would be very much appreciated!

Upvotes: 0

Views: 119

Answers (2)

Sherm Pendley
Sherm Pendley

Reputation: 13612

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?

Upvotes: 1

Matteo Alessani
Matteo Alessani

Reputation: 10412

You need to compare string with isEqualToString:

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

Upvotes: 3

Related Questions