3
NSMutableString  *selectDay=@"Wed 14 May";
[selectDay stringByAppendingFormat:@"%i", yearNumber];
NSLog(@"%@",selectDay);

I will tried this one.But it can't append the yearNumber to that String please help me.YearNumber contain 2011.

2
  • warning: incompatible Objective-C types initializing 'struct NSString *', expected 'struct NSMutableString *' wont you get this warning?? Commented May 5, 2011 at 7:03
  • you have asked six questions, none accepted. Please accept the answer that solves your problem by clicking the tick mark below the downvote arrow. Otherwise you may not receive better help from this community. Commented May 5, 2011 at 7:08

6 Answers 6

6

stringByAppendingFormat: returns the new string, it does not modify the receiver string. That's why you are getting no change. Try this:

NSMutableString  *selectDay=@"Wed 14 May";
NSString *newString = [selectDay stringByAppendingFormat:@"%i", yearNumber];
NSLog(@"%@", newString);

Or this:

NSMutableString  *selectDay=@"Wed 14 May";
NSString *newString = [NSString stringWithFormat:@"%@%i", selectDay, yearNumber];
NSLog(@"%@", newString);

EDIT: Actually you don't need mutable string for this. selectDay should be a normal NSString.

NSString  *selectDay=@"Wed 14 May";
Sign up to request clarification or add additional context in comments.

Comments

2

change the below line

[selectDay stringByAppendingFormat:@"%i", yearNumber];

to

selectDay = [NSString stringWithFormat:@"%@%i", selectDay, yearNumber];

definitely it will work ...

Comments

1

You define your variable to be of type NSMutableString *, but the constant string you're passing is of type NSString * which is already wrong. There are two solutions: with or without NSMutableString.

NSMutableString *selectDay = [NSMutableString stringWithString:@"Wed 14 May"];
[selectDay appendFormat:@"%i", yearNumber];
NSLog(@"%@", selectDay);

Here the mutable string is generated from the constant string and then it is modified through appending.

NSString *selectDay = @"Wed 14 May";
NSString *newDay = [selectDay stringByAppendingFormat:@"%i", yearNumber];
NSLog(@"%@", newDay);

The point here is that stringByAppendingFormat: does not modify the original string, it returns a new one. And you simply need to "catch" it in a variable.

Comments

1

Try this:-

NSString   *selectDay=@"Wed 14 May";
    int yearNumber=2011;
    selectDay=[selectDay stringByAppendingFormat:[NSString stringWithFormat:@"%d", yearNumber]];
    NSLog(@"%@",selectDay);

1 Comment

Point is, stringByAppendingFormat: does not modify selectDay, it returns a new string (which is simply lost as it's not assigned to a variable).
1
   NSString *selectDay=@"Wed 14 May";
    NSString *appendedString = [NSString stringWithFormat:@"%@ %d",selectDay, yearNumber];

    NSLog(@"%@",appendedString);

Try this

2 Comments

I also added a comment for "using a constant NSString to initialize a pointer to NSMutableString is plain wrong" in the question
Have deleted my previous comment after you've modified your answer.
0

You can try:

NSString *selectDay = [NSString stringWithFormat:@"Wed 14 May %d", yearNumber];

Comments

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.