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

_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [EMAIL PROTECTED]

Reply via email to