Home > DeveloperSection > Forums > How to use coverted nsstring into FSCreateDirectoryUnicode?
Royce Roy
Royce Roy

Total Post:149

Posted on    January-11-2016 1:01 AM

 iPhone iPhone  Ios  Objective C 

 1 Reply(s)
 466  View(s)
Rate this:
I'm a beginner in Objective-C, I am trying this code in our app but its not working correctly. I think on calling FSCreateDirectoryUnicode- I don not get the correct length.
Can you tell me how can I get the correct length or what will be the simplest way to do it?
this is my code:
NSString *strObj = @"DirectoryName";
NSData *dataObj = [strObj dataUsingEncoding:NSUnicodeStringEncoding
FSCreateDirectoryUnicode(&aFolderFSRef, [strObj length], [dataObj bytes],
kFSCatInfoNone, NULL, &newFolderFSRef, NULL, NULL);

Tarun Kumar

Total Post:214

Posted on    January-13-2016 6:53 AM

I found many issues with your source string data. In Cocoa the easiest way to do that is:
NSString *strObj = @"DirectoryName";
NSString *strPath = [NSHomeDirectory() stringByAppendingPathComponent:strObj];
[[NSFileManager defaultManager] createDirectoryAtPath:strPath attributes:nil];
We can use FSRef for specifying the created directory path. In our example we use home directory instead. If we really use the directory in the FSRef & don't know the path, it will be easier to use the FSCreateDirectoryUnicode function:
Here is the correct encoding of the above code:
NSString *strObj = @"DirectoryName";
const UniChar *name = (const UniChar*)[strObj cStringUsingEncoding:NSUnicodeStringEncoding];
FSCreateDirectoryUnicode(&aFolderFSRef, [strObj length], name, kFSCatInfoNone, NULL, &newFolderFSRef, NULL, NULL);

Modified On Jan-13-2016 06:59:56 AM

Don't want to miss updates? Please click the below button!

Follow MindStick