FROM : Adam R. Maxwell
DATE : Sat Mar 15 20:23:18 2008
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
DATE : Sat Mar 15 20:23:18 2008
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
| Related mails | Author | Date |
|---|---|---|
| Kevin Dixon | Mar 15, 20:04 | |
| Adam R. Maxwell | Mar 15, 20:23 | |
| Jean-Daniel Dupas | Mar 15, 20:25 | |
| Keith Duncan | Mar 15, 20:34 | |
| Adam R. Maxwell | Mar 15, 20:48 | |
| Stuart Malin | Mar 16, 07:49 | |
| stephen joseph but… | Mar 16, 09:11 | |
| Nir Soffer | Mar 16, 10:41 | |
| Jean-Daniel Dupas | Mar 16, 12:09 | |
| Jens Alfke | Mar 16, 16:35 | |
| Stuart Malin | Mar 16, 18:09 | |
| Michael Ash | Mar 16, 19:15 | |
| Adam R. Maxwell | Mar 16, 19:38 | |
| Kevin Dixon | Mar 17, 16:44 | |
| Sean McBride | Mar 17, 17:27 |






Cocoa mail archive

