I have an NSString
that I'd like to append a {
at the beginning of it.
NSMutableString *str = [[NSMutableString alloc] initWithString:@"{"];
[str stringByAppendingString:extracted];
This returns str
with only {
. Why is that? Objective-C is VERY frustrating with how it provides many ways of doing the same thing, yet for situations a different way is required.
I tried doing [NSMutableString string]
and appending {
then extracted
and it still does not work. Why is this not working and what should I do?
stringByAppendingString
returns a new string, it does not modify the old string.
All functions that begins with stringWith
or arrayWith
etc. create new objects rather than modifying old objects.
To acheive what you want, a simpler solution is:
NSString *str = [NSString stringWithFormat:@"{%@", oldString];
or:
NSMutableString *str = [@"{" mutableCopy];
[str appendString:blah];
You are calling an NSString
method that returns a new string and does not modify the string that it is called on. You need to call appendString
.
[str appendString:extracted];
// use it like this
NSString *extracted = @"this is my string";
NSString *str = [[NSString alloc] initWithString:@"{"];
str = [str stringByAppendingString:extracted];
Hope this will help you
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.