Re: Why isn't this working???? (NSURL and files)
Re: Why isn't this working???? (NSURL and files)
- Subject: Re: Why isn't this working???? (NSURL and files)
- From: Dustin Voss <email@hidden>
- Date: Tue, 1 Apr 2003 19:39:21 -0800
On Tuesday, April 1, 2003, at 06:35 PM, Francisco Tolmasky wrote:
I'm trying to work with NSURL, but for some reason it's not working.
WHen I convert from a string to an NSURL it isn't doing it or
something. Here is my code:
NSMutableDictionary *_assignmentTypesDictionary;
//...
+ (NSMutableDictionary *)types
{
if(!_assignmentTypesDictionary)
{
NSString *str= [[[NSFileManager defaultManager]
findSystemFolderType:kApplicationSupportFolderType
forDomain:kUserDomain] stringByAppendingString:@"types.gds"];
You'll want to append your CFBundleName to the Application Support
path. You can use this code:
CFDictionaryRef localizedInfo = CFBundleGetLocalInfoDictionary
(CFBundleGetMainBundle());
CFStringRef bundleName = (CFStringRef) CFDictionaryGetValue
(localizedInfo, CFSTR("CFBundleName"));
This code retrieves the localized bundle name. This might be the same
as the preference file; I don't know whether OS X uses the localized or
un-localized bundle name for that. An alternative is to use the app
name. On the one hand, bundle names are more unique, but on the other
hand, everybody is currently using app names. I couldn't find
clarification from Apple on which to use.
Opinions?
NSURL *typesFile= [NSURL URLWithString: [@"file://"
stringByAppendingString: str]];
Use this instead. It probably does various encoding conversions for
you, plus it's shorter:
NSURL *typesFile = [NSURL fileURLWithPath: str];
if([[NSFileManager defaultManager] fileExistsAtPath:
[typesFile
path]])
_assignmentTypesDictionary= [NSMutableDictionary
dictionaryWithContentsOfURL: typesFile];
else
{
_assignmentTypesDictionary= [[NSMutableDictionary alloc]
initWithObjectsAndKeys:
The dictionary you just made is not auto-released. Use
"dictionaryWithObjectsAndKeys:" instead.
[NSColor redColor], @"Homework", [NSColor blueColor],
@"Quiz", [NSColor greenColor], @"Test",
[NSColor yellowColor], @"Lab", nil];
[_assignmentTypesDictionary writeToFile: [typesFile path]
atomically: YES];
Use "writeToURL:atomically:" instead. If you're going to use URLs, use
'em.
}
}
return _assignmentTypesDictionary;
}
For some reason, it does not write the file, and if I say
NSLog([typesFile path]) nothing is printed. I've tried without
@"file://" and also with @"file:/". I've also tried using writeToURL
instead of writeToFile:.
Just guessing, but this might be because you didn't use
"fileURLWithPath:".
Note: [[NSFileManager defaultManager]
findSystemFolderType:kApplicationSupportFolderType
forDomain:kUserDomain];
just returns @"Users/name/Library/Application\ Support/"
Right, that's what I'd expect. Not what I'd PREFER (I think you ought
to be able to get the directory-plus-bundle or whatever), but if wishes
were horses...
Also, is there any way of checking to see if a directory exists? And
how do u make directories?
[NSFileManager fileExistsAtPath:isDirectory:] and [NSFileManager
createDirectoryAtPath:attributes:] respectively.
Me, I use Carbon for my file-handling needs, because FSRefs and aliases
are better for what I'm doing.
In conclusion, Read The Fargin' Manual, and Bob's your uncle.
_______________________________________________
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.