HOW TO USE COVERTED NSSTRING INTO FSCREATEDIRECTORYUNICODE?

Royce Roy

Total Post:149

Points:1043
Posted by  Royce Roy
 509  View(s)
Ratings:
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
allowLossyConversion:NO];
FSCreateDirectoryUnicode(&aFolderFSRef, [strObj length], [dataObj bytes],
kFSCatInfoNone, NULL, &newFolderFSRef, NULL, NULL);
  1. Tarun Kumar

    Post:214

    Points:1642
    Re: How to use coverted nsstring into FSCreateDirectoryUnicode?

    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

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!