On May 26, 2009, at 8:33 AM, vinai wrote:

I think I am missing something really basic here with NSString.

Actually, it seems you're missing stuff much more basic than that. You seem to not understand the difference between a pointer and an object (which a pointer might point to).


if ([oPanel runModalForDirectory:nil file:nil types:nil] == NSOKButton)
   {
       NSArray * files = [oPanel filenames];

       /* Process files */

       for( i = 0; i < [files count]; i++ )
       {
[rawFileName initWithString: [[files objectAtIndex:i] stringByStandardizingPath]];

This is very atypical. You should not be init-ing an object except immediately upon allocating it. Typically, this is done in an expression which combines the +alloc message with a -init... message. So, for example:

rawFileName = [[NSString alloc] initWithString: [[files objectAtIndex:i] stringByStandardizingPath]];

That bring us to issues of memory management, for which you should read the memory management guide. In this case, you would have to make sure you release the object pointed to by rawFileName when you're done with it.

However, why are you attempting to init another object, at all? The expression [[files objectAtIndex:i] stringByStandardizingPath] returns a pointer to a string object, already. There's a pretty good chance that you can just make use of that string object directly. If you need to keep it around, you could retain it and then release it later.

           NSLog(@"Logging the file name variable");
           NSLog(rawFileName);

           NSLog(@"Logging the called file path object directly");
           NSLog([[files objectAtIndex:i] stringByStandardizingPath]);
       }
   }

and this is the output from that section of code:

2009-05-26 09:18:41.931 REMI[19710:807] Logging the file name variable
2009-05-26 09:18:41.932 REMI[19710:807] Logging the called file path object directly 2009-05-26 09:18:41.932 REMI[19710:807] /Users/vinai/Desktop/ rawTestData/P04608.7

rawFileName is declared to be an NSString * in my object's header file.

And here is why I think you misunderstand the difference between a pointer and an object which it might point to.

Having declared a pointer as an instance variable means that each of your objects has a pointer. It does not mean that each of your objects has a string object. Unless you explicitly assign that pointer to point to something, it points to nothing.

So why does printing out the string returned by the stringByStandardizingPath function work okay, but not when initializing an NSString * variable with it ?

You did not initialize the rawFileName variable with the above code. You attempted to initialize the object to which rawFileName pointed. There are two problems: 1) rawFileName points to nothing, and 2) if it did point to something, that object should have already been initialized and you're not allowed to initialize it again.

You can choose a couple of alternative approaches:

*) You could change what rawFileName points to by assigning it the address of a different object. If you do that, be sure to get memory management right. *) You could have rawFileName point to a mutable string object, and change the content of that string object. That's different from attempting to reinitialize the object.

Regards,
Ken

_______________________________________________

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 arch...@mail-archive.com

Reply via email to