0
NSString *string = @"HELLO";

For some reason, XCode won't auto-complete methods like remove characters or append etc... If that's the case, how can I, say, remove certain characters from my string? Say I want to remove all the L's.

Saturn
  • 17,888
  • 49
  • 145
  • 271

2 Answers2

5

NSString doesn't respond to those methods. NSMutableString does, but you've declared an immutable string variable and assigned to it a string literal. Since an Objective-C @"string literal" is always immutable (an instance of NSString but not NSMutableString), there's no way those messages can be sent to the object you're using.

If you want a mutable string, try:

NSMutableString *mutableString = [[@"HELLO" mutableCopy] autorelease];
Jonathan Grynspan
  • 43,286
  • 8
  • 74
  • 104
1

That's an immutable string literal.

Here is a great post explaining it in further details:

What's the difference between a string constant and a string literal?

As for your question on how would you change it and remove the Ls:

NSString *hello = @"HELLO";
NSString *subString = [hello stringByReplacingOccurrencesOfString:@"L" withString:@""];
NSLog(@"subString: %@", subString);

That outputs "HEO"

Either that or you can create an NSMutableString by creating a copy of the mutable string like Jonathan mentioned. In both examples, you're copying it into a non-literal string.

Community
  • 1
  • 1
bryanmac
  • 38,941
  • 11
  • 91
  • 99