I’m new to Mac and Objective-C, so I may be barking up the wrong tree here and quite possibly there are better ways of doing this.
I have tried the code below and it doesn’t seem right. It seems I don’t get the correct length in the call to FSCreateDirectoryUnicode. What is the simplest way to accomplish this?
NSString *theString = @"MyFolderName";
NSData *unicode = [theString dataUsingEncoding:NSUnicodeStringEncoding allowLossyConversion:NO];
FSCreateDirectoryUnicode(&aFolderFSRef, [theString length], [unicode bytes], kFSCatInfoNone, NULL, &newFolderFSRef, NULL, NULL);
There are a couple of issues with your raw string data. But the easiest way to do it in Cocoa is:
You did use an FSRef to specify the path to where the directory was created. My example uses the home directory instead. If you really have to use the directory in the FSRef and do not know the path to it, it might be easier to use the FSCreateDirectoryUnicode function:
Edit: changed code to use correct encoding.
The only thing that was broken in the original code, was that
dataUsingEncodingreturns the external representation of the string. That means that the data contains a unicode byte order mark at the beginning, which FSCreateDirectoryUnicode does not want.