Monday, April 22, 2013

NSFileManager fileSystemRepresentationWithPath example ios


fileSystemRepresentationWithPath:

Returns a C-string representation of a given path that properly encodes Unicode strings for use by the file system.
- (const char *)fileSystemRepresentationWithPath:(NSString *)path
Parameters
path
A string object containing a path to a file. This parameter must not be nil or contain the empty string.
Return Value
A C-string representation of path that properly encodes Unicode strings for use by the file system.
Discussion of [NSFileManager fileSystemRepresentationWithPath]
Use this method if your code calls system routines that expect C-string path arguments. If you use the C string beyond the scope of the current autorelease pool, you must copy it.
This method raises an exception if path is nil or contains the empty string. This method also throws an exception if the conversion of the string fails.
Example of [NSFileManager fileSystemRepresentationWithPath]

#import "MyObject.h"
@implementation MyObject
- (IBAction)myAction:(id)sender
{
//OSType *ost;
NSFileManager *myFile = [ NSFileManager defaultManager];
const char *cStr;
    NSArray      *imgTypes    = [ NSArray arrayWithObject : @"tiff" ];
    NSOpenPanel  *opImage       = [ NSOpenPanel openPanel ];
    int    opRet;
     
    opRet = [ opImage runModalForDirectory : NSHomeDirectory()
                                     file : @"Pictures"
                                    types : imgTypes ];
    if ( opRet == NSOKButton ) { 
cStr = [myFile fileSystemRepresentationWithPath:[[ opImage filename ] stringByExpandingTildeInPath]];
NSLog([NSString stringWithCString:cStr]);
    }
}
@end