Re: Convert NSString to FSRef
Re: Convert NSString to FSRef
- Subject: Re: Convert NSString to FSRef
- From: Jean-Daniel Dupas <email@hidden>
- Date: Sun, 16 Mar 2008 12:09:02 +0100
Le 16 mars 08 à 07:49, Stuart Malin a écrit :
Kevin,
Here's two variants that you can start to work with. Use the first
one if it works. The second one is a more roundabout way of getting
the NSString to bytes.
Two disclaimers:
1: I have never worked with CF functions before, but have been doing
a bit of string-data-bytes manipulation in Cocoa, so this represents
my suggested starting point based on knowing how to transform the
NSString into accessible bytes.
2: I have compiled, but not executed, so I have no idea if either of
these work.
Please note that I have added a suffix (A/B) to the function names
to distinguish them.
- (FSRef)stringToFSRefA: (NSString*)filePath {
FSRef output;
CFURLRef url;
// convert the NSString to a C-string
const char *filePathAsCString = [filePath UTF8String];
CFURLRefCreateWithBytes(
kCFAllocatorDefault, // CFAllocatorRef
filePathAsCString, // the bytes
strlen(filePathAsCString), // the length
kCFStringEncodingUTF8, // encoding
NULL); // CFURLRef baseURL
CFURLGetFSRef(url, &output);
return output;
}
- (FSRef)stringToFSRefB: (NSString*)filePath {
FSRef output;
CFURLRef url;
// convert the NSString to raw bytes
NSData *filePathAsData = [filePath
dataUsingEncoding:NSUTF8StringEncoding];
const uint8_t *urlBytes = (uint8_t *)[filePathAsData bytes]; //
Note: NOT null terminated
CFURLRefCreateWithBytes(
kCFAllocatorDefault, // CFAllocatorRef
urlBytes, // the bytes
[filePathAsData length], // the length
kCFStringEncodingUTF8, // encoding
NULL); // CFURLRef baseURL
CFURLGetFSRef(url, &output);
return output;
}
HTH,
--Stuart
Maybe you should read "Memory Management in CoreFoundation" guide…
_______________________________________________
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