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.
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.
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];
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.