[NSString stringByAppendingPathComponent]
Returns a new string made by appending to the receiver a given string.
- (NSString *)stringByAppendingPathComponent:(NSString *)aString
Parameters
- aString
- The path component to append to the receiver.
Return Value
A new string made by appending aString to the receiver, preceded if necessary by a path separator.
Discussion of [NSString stringByAppendingPathComponent]
The following table illustrates the effect of this method on a variety of different paths, assuming that aString is supplied as “
scratch.tiff
”:
Receiver’s String Value
|
Resulting String
|
---|---|
“/tmp ” | “/tmp/scratch.tiff ” |
“/tmp/ ” | “/tmp/scratch.tiff ” |
“/ ” | “/scratch.tiff ” |
“” (an empty string) | “scratch.tiff ” |
Note that this method only works with file paths (not, for example, string representations of URLs).
Example of [NSString stringByAppendingPathComponent]
NSMutableString* mutablePath = [NSMutableString string];
NSString* fullPath = [rootPath stringByAppendingPathComponent:filename];
[mutablePath setString:fullPath]; // OK to setString: of Mutable with non-Mutable
[mutablePath appendString:someOtherString]; // This won't cause an exception
// Example to clarify on comments below
{
// This will cause a compiler warning.
// warning: incompatible Objective-C types assigning
// ‘struct NSString *’, expected ‘struct NSMutableString *’
NSMutableString* ms = [@"FOO" stringByAppendingPathComponent:@"BAR"];
}
Example of [NSString stringByAppendingPathComponent]
if (imagePath) {
[imagePath release];
}
imagePath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@" %@.png", photoName]];
[imagePath retain];
Example of [NSString stringByAppendingPathComponent]
NSString* string1 = [[self baseURL] stringByAppendingString:partial];
NSString* string2 = [[self baseURL] stringByAppendingPathComponent:partial];
NSLog(@"string1 is %s", [string1 UTF8String]);
NSLog(@"string2 is %s", [string2 UTF8String]);