1
NSMutableString *a = @"Hi";

NSMutableString *b =[a stringByAppendingString:@"\n\n Hi Again"];

The above doesn't give an error but does not put "Hi Again" on the next line. Why?

Michael Petrotta
  • 59,888
  • 27
  • 145
  • 179
Michael
  • 11
  • 1
  • 2

4 Answers4

1

I think this might help you. You'd rather to use '\r' instead of '\n'

I also had a similar problem and found \n works in LLDB but not in GDB

Community
  • 1
  • 1
Joon Hong
  • 1,337
  • 15
  • 23
1

EDIT2 I realised after posting, that the OP had NSString in the title but put NSMutableString in the code. I have submitted an edit to change the NSMutableString to NSString.

I will leave this as it still maybe helpful.


Well I am surprised that does not give an error, because you are giving a NSMutableString a NSString.

You need to read the Documentation on NSMutableStrings.

to give you an idea

//non mutable strings
  NSString *shortGreetingString = @"Hi";
NSString *longGreetingString =  @"Hi Again";
 /*mutable string - is created and given a character capacity The number of characters indicated by capacity is simply a hint to increase the efficiency of data storage. The value does not limit the length of the string
*/

NSMutableString *mutableString= [NSMutableString stringWithCapacity:15];

/*The mutableString, now uses an appendFormat to construct the string
     each %@ in the Parameters for the appendFormat is a place holder for values of NSStrings
     listed in the order you want after the comma. 
    Any other charactars will be included in the construction, in this case the new lines.
*/
[mutableString appendFormat:@"%@\n\n%@",shortGreetingString,longGreetingString];


NSLog (@"mutableString = %@" ,mutableString);

[pool drain];
markhunte
  • 6,805
  • 2
  • 25
  • 44
0

Try using NSString. You could use:

NSString *a = [NSString stringWithFormat:@"%@\n\n%@", @"Hi", @"Hello again"]
Sam Ritchie
  • 10,988
  • 4
  • 42
  • 53
Kevin Chavez
  • 949
  • 1
  • 11
  • 27
  • Please accept an answer, also did you mean NSString or NSMutableString. You title and code do not match. Cheers – markhunte Mar 28 '11 at 10:00
0

If your string is going in a UIView (e.g a UILabel), you also need to set the number of lines to 0

myView.numberOfLines=0;
Andy A
  • 4,191
  • 7
  • 38
  • 56