Understanding array pointer
I'm new to Objective-C and need help with the concept of pointers. I've written this code:
//myArray is of type NSMutableArray
NSString *objectFromArray = [myArray objectAtIndex:2];
[objectFromArray uppercaseString];
I assumed that this would change the string at myArray[2] since I got the actual pointer to it. Shouldn't any changes to the dereferenced pointer mean that the obje开发者_Go百科ct in that location changes? Or does this have something to do with 'string immutability'? Either way, when I use NSLog and iterate through myArray, all the strings are still lowercase.
Shouldn't any changes to the dereferenced pointer mean that the object in that location changes?
Yes, they would. But if you read the documentation for uppercaseString
, you see that it does not modify the string in place. Rather, it returns a new uppercase version of the original string. All methods on NSString
work like that.
You would need an instance of NSMutableString
to be able to modify its contents in place. But NSMutableString
does not have a corresponding uppercase method, so you would have to write it yourself (as a category on NSMutableString
).
of course!! no string in the array will be converted to uppercase as the statement [objectFromArray uppercaseString]; would have returned the uppercase string which was not collected in any object though. "uppercaseString" does not modify the string object itself with which is is called...!!
精彩评论