RE: NSPopUpButton failure after adding file names
RE: NSPopUpButton failure after adding file names
- Subject: RE: NSPopUpButton failure after adding file names
- From: "Jonathan E. Jackel" <email@hidden>
- Date: Thu, 15 May 2003 12:18:27 -0400
>
- (void)populateTemplateMenu
>
{
>
NSDirectoryEnumerator *theDirectory = [[NSFileManager
>
defaultManager] enumeratorAtPath:@"templates/layouts/"];
>
NSMutableString *pathname = [[NSMutableString alloc] init];
The last line ought to be:
NSString *pathname;
There is no need to use a mutable string here. You will simply be moving a
pointer from one string provided by the enumerator to another.
You do not need to allocate any memory for this string; the enumerator will
do that. The enumerator will also handle releasing that string.
>
NSMutableString *fullPath = [[NSMutableString alloc] init];
If you know you are going to autorelease anyway, you might as well
autorelease at the time of alloc so you don't forget:
NSMutableString *fullPath = [[[NSMutableString alloc] init]
autorelease];
or even better
NSMutableString *fullPath = [NSMutableString stringWithCapacity:10];
which is autoreleased for you. The capacity is just a guess and does not
limit the size of the string.
>
>
while (pathname = [theDirectory nextObject])
>
{
>
// establish the full path to the file
>
[fullPath setString:
>
@"/Users/rick/Development/pf/build/templates/layouts/"];
>
[fullPath appendString: pathname];
I think it would be better to define the constant string outside the while
loop and use another variable. Why set the same value 30 or 300 times?
Alternatively, you might want to look at NSString's rather extensive set of
methods for dealing with paths, such as
fullPath = [@"/Users/rick/Development/pf/build/templates/layouts"
stringByAppendingPathComponent:pathname];
If you go this route, you do not need fullPath to be mutable and you should
not allocate memory for it; just declare it, like pathname.
>
// isFilePackageAtPath requires full path
>
if ( ([[NSWorkspace
>
sharedWorkspace]isFilePackageAtPath:fullPath]) ||
>
([pathname isEqualToString:@".DS_Store"]) )
>
{
>
// skip any folders or bundles
>
[theDirectory skipDescendents];
>
}
>
else
>
{
>
// use the file name to build the layout menu
>
[storySectionMenu addItemWithTitle: pathname];
>
}
>
}
>
>
[theDirectory autorelease];
This is your real problem. theDirectory is given to you as an autoreleased
object. You autorelease it again, which is a no-no.
>
[pathname autorelease];
>
[fullPath autorelease];
>
}
>
Here's how I would write your code (written in Mail; not guaranteed
bug-free):
- (void)populateTemplateMenu
{
NSDirectoryEnumerator *theDirectory = [[NSFileManager
defaultManager] enumeratorAtPath:@"templates/layouts/"];
NSString *pathname, *fullPath;
while (pathname = [theDirectory nextObject])
{
// establish the full path to the file
fullPath = [@"/Users/rick/Development/pf/build/templates/layouts"
stringByAppendingPathComponent:pathname];
if ( ([[NSWorkspace
sharedWorkspace]isFilePackageAtPath:fullPath]) ||
([pathname isEqualToString:@".DS_Store"]) )
{
// skip any folders or bundles
[theDirectory skipDescendents];
}
else
{
// use the file name to build the layout menu
[storySectionMenu addItemWithTitle: pathname];
}
}
}
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.