Re: What, exactly constitutes a mutable action on an instance?
Re: What, exactly constitutes a mutable action on an instance?
- Subject: Re: What, exactly constitutes a mutable action on an instance?
- From: Lee Ann Rucker <email@hidden>
- Date: Tue, 28 May 2013 11:02:14 -0700
On May 28, 2013, at 7:44 AM, Alex Zavatone wrote:
>
> On May 28, 2013, at 9:46 AM, Steve Mills wrote:
>
>> On May 28, 2013, at 08:39:21, Alex Zavatone <email@hidden> wrote:
>>
>>> Though it's clearly defined in the docs when to use NSMubleAnything vs. NSAnything (insert Array, Dictionary, String, etc for Anything), there is no compiler warning when you perform a simple action such as allocate a string and then reassign values to it.
>>>
>>> With this in mind, what exactly constitutes a mutable action?
>>>
>>> If we take this:
>>>
>>> NSString *myString;
>>> myString = @"Hi";
>>> myString = @"Hi there";
>>>
>>> I'm clearly expecting some type of warning from the compiler when myString is redefined, but I don't see one in Xcode 4.6.1. Is this redefinition not a mutable action? It sure seems like it is.
>>
>> The example you've given is not changing the string, it's simply pointing the string pointer to a new string (changing the address it points to). This would require a mutable string:
>>
>> [myString appendString:@"Hi there"];
>>
>> because it's changing the string, but it will leave myString at the same pointer address.
>
> Excellent. This is part of the information that I'm looking for.
To toss in another complication: the object assigned to an NSString or NSMutableString variable might not be an object of that type - unlike C++, ObjC doesn't care (it might not even be an NSString at all, but in that case something odd probably happened).
Hence the warnings not to inspect the result of a method returning NSString - it might happen to be an NSMutableString but you should not try to mutate it. And if you somehow get an NSString object in an NSMutableString variable, the compiler will be happy but mutable methods will fail at runtime.
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden