Sunday, May 19, 2013

NSString stringByAbbreviatingWithTildeInPath example ios


[NSString stringByAbbreviatingWithTildeInPath]

Returns a new string representing the receiver as a path with a tilde (~) substituted for the full path to the current user’s home directory.
- (NSString *)stringByAbbreviatingWithTildeInPath
Return Value of [NSString stringByAbbreviatingWithTildeInPath]
A new string representing the receiver as a path with a tilde (~) substituted for the full path to the current user’s home directory. Returns a new string matching the receiver if the receiver doesn’t begin with a user’s home directory.
Discussion of [NSString stringByAbbreviatingWithTildeInPath]
Note that this method only works with file paths (not, for example, string representations of URLs).
Example of [NSString stringByAbbreviatingWithTildeInPath]
 NSString *Path = @"~/NSData.txt";
    NSString *absolutePath = [Path stringByExpandingTildeInPath];
    NSLog(@"absolutePath:%@",absolutePath);
    NSLog(@"Path:%@",[absolutePath stringByAbbreviatingWithTildeInPath]);

Example of [NSString stringByAbbreviatingWithTildeInPath]
-(void) finishedSearchForApplications:(NSNotification*) notification
{
    //get the number of items in the results
    NSUInteger resultCount = [[query results] count];
    NSMutableArray* appsFound = [NSMutableArray array];

    //loop through the results and add their paths to an array
    NSUInteger i;
    for(i=0;i<resultCount;i++)
    {
        id queryResult=[query resultAtIndex:i];
        NSString* pathOfItem = [[[[queryResult valueForKey:@"kMDItemPath"] stringByStandardizingPath] stringByAbbreviatingWithTildeInPath] stringByDeletingPathExtension];
        NSDictionary* fileInfo = [NSDictionary dictionaryWithObject:pathOfItem forKey:@"path"];
        [appsFound addObject:fileInfo];
    }
    NSLog(@"%ld apps found: %@",resultCount,appsFound);
    //do something with appsFound
}