Sunday, May 19, 2013

NSString stringByStandardizingPath example ios


[NSString stringByStandardizingPath]

Returns a new string made by removing extraneous path components from the receiver.
- (NSString *)stringByStandardizingPath
Return Value
A new string made by removing extraneous path components from the receiver.
Discussion of [NSString stringByStandardizingPath]
If an invalid pathname is provided, stringByStandardizingPath may attempt to resolve it by calling stringByResolvingSymlinksInPath, and the results are undefined. If any other kind of error is encountered (such as a path component not existing), self is returned.
This method can make the following changes in the provided string:[NSString stringByStandardizingPath]
  • Expand an initial tilde expression using stringByExpandingTildeInPath.
  • Reduce empty components and references to the current directory (that is, the sequences “//” and “/./”) to single path separators.
  • In absolute paths only, resolve references to the parent directory (that is, the component “..”) to the real parent directory if possible usingstringByResolvingSymlinksInPath, which consults the file system to resolve each potential symbolic link.[NSString stringByStandardizingPath]
    In relative paths, because symbolic links can’t be resolved, references to the parent directory are left in place.
  • Remove an initial component of “/private” from the path if the result still indicates an existing file or directory (checked by consulting the file system).
Note that the path returned by this method may still have symbolic link components in it. Note also that this method only works with file paths (not, for example, string representations of URLs).
Example of [NSString stringByStandardizingPath]
-(NSData*)loadData:(NSString*)fileName
{
    NSString *dataPath = [self.path stringByAppendingPathComponent:fileName];
    dataPath = [dataPath stringByStandardizingPath];
    NSData *data = [[[NSData alloc] initWithContentsOfFile:dataPath]autorelease ];
    return data;
}
Example of [NSString stringByStandardizingPath]
- (void) exportProfile:(Profile *)profile toPath:(NSString *)path{
    //Call the NSManagedobject function to export
    NSDictionary *profileDict = [self.selectedProfile dictionaryForExport];

    if([profileDict writeToFile:[path stringByStandardizingPath] atomically:YES]) 
        NSLog(@"Saved");
    else
        NSLog(@"Not saved");
}
Example of [NSString stringByStandardizingPath]
- (void) importProfileFromPath:(NSString *)path{
    NSManagedObjectContext *context = [self.fetchedResultsController managedObjectContext];
    Profile *newProfile = [NSEntityDescription insertNewObjectForEntityForName:@"Profile" inManagedObjectContext:context];

    //load dictonary from file
    NSMutableDictionary *profileDict = [NSMutableDictionary dictionaryWithContentsOfFile:[path stringByStandardizingPath]];
    //call the NSManagedObjects import function
    [newProfile importWithDictonary:profileDict context:context];

    NSError *error = nil;
    if (![context save:&error]) {

        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
    }
}