Sunday, May 19, 2013

NSString stringByAppendingPathExtension example ios


[NSString stringByAppendingPathExtension]

Returns a new string made by appending to the receiver an extension separator followed by a given extension.
- (NSString *)stringByAppendingPathExtension:(NSString *)ext
Parameters
ext
The extension to append to the receiver.
Return Value
A new string made by appending to the receiver an extension separator followed byext.
Discussion of [NSString stringByAppendingPathExtension]
The following table illustrates the effect of this method on a variety of different paths, assuming that ext is supplied as @"tiff":
Receiver’s String Value
Resulting String
/tmp/scratch.old/tmp/scratch.old.tiff
/tmp/scratch./tmp/scratch..tiff
/tmp//tmp.tiff
scratchscratch.tiff
Note that adding an extension to @"/tmp/" causes the result to be @"/tmp.tiff"instead of @"/tmp/.tiff". This difference is because a file named @".tiff" is not considered to have an extension, so the string is appended to the last nonempty path component.
[NSString stringByAppendingPathExtension]
This method does not allow you to append file extensions to filenames starting with the tilde character (~).
Note that this method only works with file paths (not, for example, string representations of URLs).
Example of [NSString stringByAppendingPathExtension]
// path contains the path to the folder you want to find the file in.

// create the full file name

NSString* shortPath = [@"input" stringByAppendingPathExtension: @"txt"];
NSString* fullPath = [path stringByAppendingPathComponent: shortFileName];

// open the file.  we convert to an NSURL so we can use the better open method
NSURL* fileURL = [NSURL fileURLWithPath: fullPath];
NSError* error = nil;
NSFileHandle *file=[NSFileHandle fileHandleForReadingFromURL: fileURL
                                                       error: &error];
if (file == nil)
{
    // error contains info on what went wrong
}
else
{
    // do what you need
}
Example of [NSString stringByAppendingPathExtension]
@interface NSString (MoreMagic)
- (NSString *)stringByAddingFileSuffix:(NSString *)suffix;
@end

@implementation NSString (MoreMagic)
- (NSString *)stringByAddingFileSuffix:(NSString *)suffix
{
    NSString * extension = [self pathExtension];
    NSString * baseName = [self stringByDeletingPathExtension];
    NSString * thumbBase = [baseName stringByAppendingString:suffix];
    return [thumbBase stringByAppendingPathExtension:extension];
}
@end
Example of [NSString stringByAppendingPathExtension]
NSString* directoryPath = [NSString stringWithFormat: @"%@/Files", [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex: 0]];
    [[NSFileManager defaultManager] createDirectoryAtPath:directoryPath withIntermediateDirectories:YES attributes:NO error:nil];

    NSMutableArray *FilesArray=[[NSMutableArray alloc] initWithObjects:@"path1",@"path2"]];//add file Paths To FilesArray    
    for(int i=0;i<[FilesArray count];i++)
    {
        NSString *str=[directoryPath copy];
        str=[str stringByAppendingPathExtension:[NSString stringWithFormat:@"File %i",i+1]];
        NSData *FileData = [[NSData alloc] initWithContentsOfFile:[FilesArray objectAtIndex:i]]; 
        [FileData writeToFile:str atomically:NO];
        [str release];
    }