I want to manipulate NSString in obj-c here is what I want to do :
iterate a string though a for-each / for loop and shift left (<<) each character of NSString
but I don’t know how should I iterate through the NSString’s characters and how to use shift operator in obj-c.
I’m fairly new in objective-c .
regards
NSStrings are immutable;mutableCopyWithZone:will get you an (implicitly retained)NSMutableString. However,NSMutableStringdoesn’t have a way of setting individual characters. It would be easier to get an array of characters using one of the many methods (e.g.getCharacters:range:for wide characters, orcStringUsingEncoding:,getCString:maxLength:encoding:orUTF8Stringfor c-style strings), then operate on that (note some methods return const strings), then construct a new string using (e.g.)initWithCString:encoding:. Keep in mind that, depending on what you’re trying to accomplish, shifting bytes may not give you the result you expect, due to encoding issues and multibyte characters.You can get the length of a string using
length, which is the number of characters in the string (also the size, in unichars, of a buffer to hold UTF-16 data, not including a null-terminator), orlengthOfBytesUsingEncoding:, which will tell you the size (number of bytes) needed for a buffer to hold the contents of the string (not including a null-terminator).maximumLengthOfBytesUsingEncoding:can also be used for a buffer size, though it may be larger than the actual necessary size. For variable-length encodings, the maximum size is the largest possible character size (e.g. 3 for UTF-8 encoded unichars) times the number of characters.Looping and shifting is otherwise the same as in C: initialize the index variable to the lower bound (0) and loop until the index variable exceeds the upper bound.
If the data isn’t string data but bytes,
NSData/NSMutableDatawould be more appropriate.