Created
November 30, 2011 21:27
-
-
Save mdippery/1410946 to your computer and use it in GitHub Desktop.
Python's os.path.join, for Objective-C
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <Foundation/Foundation.h> | |
#import <stdarg.h> | |
@interface NSString (Paths) | |
+ (NSString *)stringByJoiningPathComponents:(NSString *)part, ...; | |
@end | |
@implementation NSString (Paths) | |
+ (NSString *)stringByJoiningPathComponents:(NSString *)part, ... | |
{ | |
NSString *path = [NSString stringWithString:part]; | |
NSString *newPart = nil; | |
va_list args; | |
va_start(args, part); | |
while ((newPart = va_arg(args, NSString *)) != nil) { | |
path = [newPart isAbsolutePath] ? [NSString stringWithString:newPart] : [path stringByAppendingPathComponent:newPart]; | |
} | |
va_end(args); | |
return path; | |
} | |
@end | |
int main(int argc, char **argv) | |
{ | |
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; | |
NSString *path = [NSString stringByJoiningPathComponents:@"/", @"tmp", @"static", @"nybooks.com", nil]; | |
NSLog(@"%@", path); | |
[pool release]; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks! I made that modification.