Monday, May 13, 2013

NSString fileSystemRepresentation example ios


[NSString fileSystemRepresentation]

Returns a file system-specific representation of the receiver.
- (const char *)fileSystemRepresentation
Return Value of [NSString fileSystemRepresentation]
A file system-specific representation of the receiver, as described for getFileSystemRepresentation:maxLength:.
Discussion of [NSString fileSystemRepresentation]
The returned C string will be automatically freed just as a returned object would be released; your code should copy the representation or use getFileSystemRepresentation:maxLength: if it needs to store the representation outside of the autorelease context in which the representation is created.
Raises an NSCharacterConversionException if the receiver can’t be represented in the file system’s encoding. It also raises an exception if the string contains no characters.
Note that this method only works with file paths (not, for example, string representations of URLs).
To convert a char * path (such as you might get from a C library routine) to an NSString object, useNSFileManager‘s stringWithFileSystemRepresentation:length: method.
Example of [NSString fileSystemRepresentation]
NSTask *task = [NSTask new];
long size = ...; // Note! dd multiplies this by 512 by default
NSString *path = ...;
[task setLaunchPath:@"/bin/dd"];
[task setArguments:[NSArray arrayWithObjects:@"dd", @"if=/dev/zero",
    [NSString stringWithFormat:@"of=%s", [path fileSystemRepresentation]],
    [NSString stringWithFormat:@"count=%ld", size],
    nil]];
[task launch];
[task waitUntilExit];
if ([task terminationStatus] != 0) {
    // an error occurred...
}
[task release];
Example of [NSString fileSystemRepresentation]
NSString *path = @"Some... path... here";
FSRef f;
OSStatus os_status = FSPathMakeRef((const UInt8 *)[path fileSystemRepresentation], &f, NULL);

if (os_status == noErr) {
    NSLog(@"Success");
}
Example of [NSString fileSystemRepresentation]
const char *h_path_char = [[[[NSProcessInfo processInfo] arguments] objectAtIndex:1] fileSystemRepresentation];
const char *s_path_char = [[[[NSProcessInfo processInfo] arguments] objectAtIndex:2] fileSystemRepresentation];

NSString *h_path = [NSString stringWithUTF8String:h_path_char];
NSString *s_path = [NSString stringWithUTF8String:s_path_char];

NSLog(@"%@", h_path);
NSLog(@"%@", s_path);