How to use coverted nsstring into FSCreateDirectoryUnicode?

Total Post:149

 753  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);
  1. Post:214

    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 Apr-10-2018 11:59:28 PM