Skip navigation.
 
mlRe: Does this caution need fixed? (newb)
FROM : Andy Lee
DATE : Thu Jul 03 21:43:14 2008

On Jul 3, 2008, at 2:57 PM, Chris Paveglio wrote:

> My code is like this:
>
> NSMutableString *theSettings;


There's the cause of the warning.  NSMutableString is a subclass of 
NSString, which means every NSMutableString is an NSString, but not 
vice versa.  The stringByAppendingPathComponent: method returns an 
NSString.  The string it returns might theoretically be an 
NSMutableString, but there's no guarantee of that -- all you know is 
that it's an NSString -- hence the warning.  In some languages this 
would have been an error and your compilation would have failed, but 
Objective-C is more permissive.

In your code you don't actually need theSettings to be a mutable 
string.  You should change the declaration to

NSString *theSettings;

Better yet, move the declaration inside the loop, as I'll get to below.

The reason your code "worked" is that you didn't do anything with 
theSettings that required it to actually be a mutable string.

> theSettings = [[NSMutableString alloc] init];


Here you are creating an instance of NSMutableString and assigning it 
to theSettings.  Note this is still okay if you declare theSettings as 
an NSString, because as I said, an NSMutableString is an NSString.

Object inheritance works like this:

NSObject (NSObject is a root class)
    NSString (NSString is a subclass of NSObject, so every NSString 
is an NSObject)
        NSMutableString (similarly, every NSMutableString is an 
NSString)

More to the point, you don't need the string you just created.  More 
on this below.

> //myPrefs is an array of strings, each item is like "Library/Safari"
>
> int i;
>     for (i = 0; i < 8; i++
>     {
> theSettings = [NSHomeDirectory() stringByAppendingPathComponent:
> [myPrefs objectAtIndex:i]];
> ....
> }
>
> Thinking about it, do I need the alloc and init commands? Sometimes 
> I am unsure about what needs alloc or init versus what I can just 
> declare as a variable without doing that.


You use alloc and init to create a new instance of a class.  You don't 
need the instance you created, so there was actually no reason to 
create it.  (In fact, it leaks memory, but that's a separate topic 
that you do need to learn, but I'm not going to get into it right 
now.)  You're only using theSettings as a temporary variable inside 
the loop, to hold the result of each string concatenation.

As it happens, you can declare the variable and assign it a value at 
the same time.  So what I would do is remove the declaration of 
theSettings that is outside the loop, and change the statement inside 
the loop to this:

NSString *theSettings = [NSHomeDirectory() 
stringByAppendingPathComponent:[myPrefs objectAtIndex:i]];

--Andy

Related mailsAuthorDate
mlDoes this caution need fixed? (newb) Chris Paveglio Jul 3, 18:40
mlRe: Does this caution need fixed? (newb) Andy Lee Jul 3, 19:35
mlRe: Does this caution need fixed? (newb) Kyle Sluder Jul 3, 19:47
mlRe: Does this caution need fixed? (newb) Chris Paveglio Jul 3, 20:57
mlRe: Does this caution need fixed? (newb) Sherm Pendley Jul 3, 21:08
mlRe: Does this caution need fixed? (newb) Jason Stephenson Jul 3, 21:22
mlRe: Does this caution need fixed? (newb) Michael Watson Jul 3, 21:29
mlRe: Does this caution need fixed? (newb) Andy Lee Jul 3, 21:43
mlRe: Does this caution need fixed? (newb) Chris Paveglio Jul 3, 22:09
mlRe: Does this caution need fixed? (newb) Steve Christensen Jul 3, 22:46
mlRe: Does this caution need fixed? (newb) Steve Christensen Jul 3, 22:53
mlRe: Does this caution need fixed? (newb) Sean McBride Jul 3, 23:04
mlRe: Does this caution need fixed? (newb) Jason Stephenson Jul 4, 02:22
mlRe: Does this caution need fixed? (newb) Chris Paveglio Jul 9, 16:44
mlRe: Does this caution need fixed? (newb) Jens Alfke Jul 9, 17:27
mlRe: Does this caution need fixed? (newb) Brian Stern Jul 9, 21:39