Re: what am I missing with NSString ?

2009-05-26 Thread Jeffrey Oleander

> On Tue, 2009/05/26, Volker in Lists  wrote:
> From: Volker in Lists 
> Subject: Re: what am I missing with NSString ?
> To: "vinai" 
> Cc: Cocoa-dev@lists.apple.com
> Date: Tuesday, 2009 May 26, 08:41
> from the code listed I cannot tell if you alloc'ed memory
> for your NSString at all?
> 
> What is the goal you try to achieve? If you just want to
> store a single file path... with a global NSString do as
> along the lines of:
> 
> [rawFileName release];
> rawFileName = [[NSString alloc] initWithString:[[files
> objectAtIndex:i] stringByStandardizingPath]]];

The NSString reference suggests that it will return an 
object.  Since it's an instance method and not a class
method, this suggests that an alloced string must be
the receiver.

"Returns an NSString object initialized by copying
 the characters from another given string.
- (id)initWithString:(NSString *)aString
"

OTOH, stringWithString is a class method, suggesting
that it must be sent to NSString and it will take 
care of the alloc:

"Returns a string created by copying the characters
 from another given string.
+ (id)stringWithString:(NSString *)aString
"




  
___

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


Re: what am I missing with NSString ?

2009-05-26 Thread vinai


Hi All,

Thanks so much to Micheal, Volker, and esp. to Ken for the pointer/object 
refresher.

I think my mistake came from thinking that NSString's initWithString method did 
memory allocation as well, and would create a new object in one swell foop. ;-) 
 From my own trials and failures, and from what you all are saying - this is 
definitely not the case.

> 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.

This is the approach I tried, and it seems to satisfy my requirement of having 
the file name available internally to other routines.  I have now encountered 
other problems further downstream in my pipeline, but c'est la vie ...

Again, thanks all - very, very much!

cheers
vinai



  
___

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


Re: what am I missing with NSString ?

2009-05-26 Thread Ken Thomases

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


Re: what am I missing with NSString ?

2009-05-26 Thread Volker in Lists

Hi,

from the code listed I cannot tell if you alloc'ed memory for your  
NSString at all?


What is the goal you try to achieve? If you just want to store a  
single file path... with a global NSString do as along the lines of:


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


Of course that will give you the last of the selected files stored in  
the rawFileName string object. Alternatively you can use a mutable  
string and replace the contents with the selected filename.


Maybe that helps to point you in the right directions ...

Volker


Am 26.05.2009 um 15:33 schrieb vinai:



Hi Folks,

I think I am missing something really basic here with NSString.  I  
am trying to store the path of a user-selected file (from an  
NSOpenPanel) internally so other routines can act on the data within  
the file.  Here's the relevant section of code:


   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]];

   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. So why does printing out the string returned by the  
stringByStandardizingPath function work okay, but not when  
initializing an NSString * variable with it ?  What would I need to  
rawFileName so it can be used by other methods in my object ?


Thanks much all.

vinai




___

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/volker_lists%40ecoobs.de

This email sent to volker_li...@ecoobs.de


___

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


Re: what am I missing with NSString ?

2009-05-26 Thread Michael Vannorsdel
rawFileName = [[NSString alloc] initWithString:[[files  
objectAtIndex:i] stringByStandardizingPath]];


Be sure to release rawFileName before you replace it with a new path.


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

think I am missing something really basic here with NSString.  I am  
trying to store the path of a user-selected file (from an  
NSOpenPanel) internally so other routines can act on the data within  
the file.  Here's the relevant section of code:


   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]];

   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. So why does printing out the string returned by the  
stringByStandardizingPath function work okay, but not when  
initializing an NSString * variable with it ?  What would I need to  
rawFileName so it can be used by other methods in my object ?


Thanks much all.


___

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