Tuesday, April 23, 2013

NSFileManager stringWithFileSystemRepresentation example ios

Example of [NSFileManager stringWithFileSystemRepresentation]

NSString * tempDir = NSTemporaryDirectory();
if (tempDir == nil)
    tempDir = @"/tmp";

NSString * template = [tempDir stringByAppendingPathComponent: @"temp.XXXXXX"];
NSLog(@"Template: %@", template);
const char * fsTemplate = [template fileSystemRepresentation];
NSMutableData * bufferData = [NSMutableData dataWithBytes: fsTemplate
                                                   length: strlen(fsTemplate)+1];
char * buffer = [bufferData mutableBytes];
NSLog(@"FS Template: %s", buffer);
char * result = mkdtemp(buffer);
NSString * temporaryDirectory = [[NSFileManager defaultManager]
        stringWithFileSystemRepresentation: buffer
                                    length: strlen(buffer)];


stringWithFileSystemRepresentation :length:

Returns an NSString object whose contents are derived from the specified C-string path.
- (NSString *)stringWithFileSystemRepresentation:(const char *)string length:(NSUInteger)len
Parameters
string
A C string representation of a pathname.
len
The number of characters in string.
Return Value of [NSFileManager stringWithFileSystemRepresentation]
An NSString object converted from the C-string representation string with lengthlen of a pathname in the current file system.
Discussion of [NSFileManager stringWithFileSystemRepresentation]
Use this method if your code receives paths as C strings from system routines.