• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Convert NSString to FSRef
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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


References: 
 >Re: Convert NSString to FSRef (From: Stuart Malin <email@hidden>)

  • Prev by Date: Re: Convert NSString to FSRef
  • Next by Date: Re: reading data from a UDP port - questions
  • Previous by thread: Re: Convert NSString to FSRef
  • Next by thread: Re: Convert NSString to FSRef
  • Index(es):
    • Date
    • Thread