0

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!

2 Answers 2

3

You need to compare string with isEqualToString:

if ([tempDateString isEqualToString:@"x-x-x-x"]) {
  UISampleLabel.text = @"";
}
Sign up to request clarification or add additional context in comments.

Comments

1

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?

4 Comments

You can. But keep in mind, that the instance you get from objectAtIndex:1 is an autorelease object. You must retain it if you want to use it outside the method. ;-)
Thanks, but the same is true of the instance returned from +stringWithFormat:. Besides, I'm not asking for my own benefit - I'm asking n.evermind why he thinks +stringWithFormat: is required in the above. His answer might reveal a misconception we can help clear up.
Thanks for your comments. Actually, I'm still quite confused about the @"" thing. I thought I need this to indicate that we are dealing with a string, not? Is there a difference if I declare something with stringWithFormat or not?
No, there's no difference in the end result - either way, it's a string object. @"" is just a handy shortcut that lets you declare literal string values right in your code. +stringWithFormat: is useful when you want to format a bunch of stuff into a string, as in someString = [NSString stringWithFormat:@"This:%d That:%d The other:%@", this, that, theOther]. Note that the format string that you create with @"" is just an NSString instance like any other.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.