Sunday, May 19, 2013

NSString stringByExpandingTildeInPath example ios


[NSString stringByExpandingTildeInPath]

Returns a new string made by expanding the initial component of the receiver to its full path value.
- (NSString *)stringByExpandingTildeInPath
Return Value
A new string made by expanding the initial component of the receiver, if it begins with “~” or “~user”, to its full path value. Returns a new string matching the receiver if the receiver’s initial component can’t be expanded.
Discussion of [NSString stringByExpandingTildeInPath]
Note that this method only works with file paths (not, for example, string representations of URLs).
Example of [NSString stringByExpandingTildeInPath]
NSData *output1 = [NSData dataWithContentsOfFile:
                      [@"~/centralUtilOut.tmp" stringByExpandingTildeInPath]];
NSString *output = [[NSString alloc]initWithData:output1 
                                        encoding:NSUTF8StringEncoding];

NSLog(@"%@", output);
[output release];
Example of [NSString stringByExpandingTildeInPath]
 NSError *err = nil;

 NSString *filepath = @"~/Desktop/test.txt";
 filepath = [filepath stringByExpandingTildeInPath];

 NSString *file = [NSString stringWithContentsOfFile:filepath 
                                       encoding:NSUTF8StringEncoding 
                                          error:&err];

 if(!file) {

}

[textView setString:file];
Example of [NSString stringByExpandingTildeInPath]
#import <Foundation/Foundation.h>

int main(int argc, char *argv[]) {

NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

// Need to use the full path to everything
// In this example, I am using my Downloads directory
NSString *destination = [@"~/Downloads" stringByExpandingTildeInPath];
NSString *zipFile = [@"~/Downloads/demos.zip" stringByExpandingTildeInPath];


NSTask *unzip = [[NSTask alloc] init];
[unzip setLaunchPath:@"/usr/bin/unzip"];
[unzip setArguments:[NSArray arrayWithObjects:@"-u", @"-d", 
                     destination, zipFile, nil]];

NSPipe *aPipe = [[NSPipe alloc] init];
[unzip setStandardOutput:aPipe];

[unzip launch];
[unzip waitUntilExit];
[unzip release];


// You can get rid of all the NSLog once you have finish testing
NSData *outputData = [[aPipe fileHandleForReading] readDataToEndOfFile];
NSString *outputString = [[NSString alloc] initWithData:outputData encoding:NSUTF8StringEncoding];

NSLog(@"Zip File: %@", zipFile);
NSLog(@"Destination: %@", destination);
NSLog(@"Pipe: %@", outputString);
NSLog(@"------------- Finish -----------");
[outputString release];


[pool release];
return 0;
}