Re: Convert NSString to FSRef
Re: Convert NSString to FSRef
- Subject: Re: Convert NSString to FSRef
- From: "Kevin Dixon" <email@hidden>
- Date: Mon, 17 Mar 2008 11:44:00 -0400 (EDT)
- Importance: Normal
Thanks for all the feed back, its been most helpful.
-Kevin
>
> On Mar 15, 2008, at 12:04 PM, Kevin Dixon wrote:
>
>> I'm trying to write a method that will convert a NSString containing a
>> file system URL to an FSRef with the following code
>>
>> - (FSRef)stringToFSRef: (NSString*)filePath {
>> FSRef output;
>>
>> CFStringRef cfFilePath = CFStringCreateWithCString(NULL, [filePath
>> cString], (CFStringEncoding)8);
>
> -cString has been deprecated for a very long time; it's better to use
> UTF8String. In this case, however, you can cast (NSString *) to
> CFStringRef. Check the docs on toll-free bridging.
>
>> CFURLRef url = CFCreateURLWithString(NULL, cfFilePath, NULL);
>>
>> CFURLGetFSRef(url, &output);
>>
>> return output;
>> }
>>
>> I am getting a warning on the CFCreateURLWithString, saying
>> "initialization makes pointer from integer without a cast"
>
> There's no such function. You probably meant CFURLCreate... instead.
> For an easier solution, try this:
>
> NSURL *aURL = [NSURL fileURLWithPath:filePath];
> CFURLGetFSRef((CFURLRef)aURL, &output);
>
> which also fixes your CFString and CFURL leaks.
>
> Another problem is that you have no way of returning an error in case
> CFURLGetFSRef fails, so you might want to either return the FSRef by
> reference and return a BOOL or pass in a BOOL * to return a failure
> value by reference.
>
> hth,
> Adam
>
_______________________________________________
Cocoa-dev mailing list (email@hidden)
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:
This email sent to email@hidden