Re: Convert NSString to FSRef
Re: Convert NSString to FSRef
- Subject: Re: Convert NSString to FSRef
- From: Stuart Malin <email@hidden>
- Date: Sat, 15 Mar 2008 20:49:33 -1000
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
On Mar 15, 2008, at 7:48 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);
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"
I'm not sure what it is referring to. I've tried casting all of the
arguments to their required types, and the warning persists.
Also, is there a better way to accomplish this?
_______________________________________________
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