Reputation: 2737
I would like to wipe a NSString object in my code (replace every characters by 0).
To do that I tried this :
NSString *myString;
for (int i=0; i<[myString length]; i++)
{
myString[i] = 0;
}
But it doesn't compile : "Incompatible types in assignment" at line myString[i] = 0;
I understand, but I can't use function stringByReplacingOccurancesOfString because it creates a new object and I would like to replace characters in my first object.
Any help ?
Upvotes: 1
Views: 3803
Reputation: 28572
NSString
is immutable, meaning that you can not change its contents once its created. You have NSMutableString for that purpose.
Also, you can not access the characters in a NSString
or NSMutableString
with this syntax:
myString[i] = 0;
NSString
s and NSMutableString
s are objects and you work with objects by sending messages to them. For instance, you have characterAtIndex:
method that returns the character at the given index, or NSMutableString
's replaceOccurrencesOfString:withString:options:range:
that replaces all occurrences of a given string in a given range with another given string.
Upvotes: 7
Reputation: 3939
By Definition, NSString is an immutable class, so the data inside of it is unchangeable.
You are looking for the NSMutableString class, which implements the following method:
replaceOccurrencesOfString:withString:options:range:
Which you can use to replace characters or substrings within the object you send that message to.
Upvotes: 2