Skip to content

Instantly share code, notes, and snippets.

@GeneralD
Created July 12, 2014 09:23
Show Gist options
  • Save GeneralD/622cc517ee4ee680dd05 to your computer and use it in GitHub Desktop.
Save GeneralD/622cc517ee4ee680dd05 to your computer and use it in GitHub Desktop.
Objective-C唯一のiOSファイルパスライブラリ、YKFileの説明書 ref: http://qiita.com/GeneralD/items/b8bd37b1f698ee98fe58
pod "YKFile"
YKFile *file = [YKFile documentsDirectory];
[file changeDirectory:@"Assets"];
[file makeDirectory];
YKFile *file = [YKFile documentsDirectory];
[file makeDirectory:@"Assets"];
YKFile *file = [YKFile documentsDirectory];
[file cd: @"abc/def/*/*"];
[UIImage imageWithContentsOfFile:file.fullPath];
YKFile *file = [YKFile documentsDirectory];
[file changeDirectory:@"Assets"];
YKFile *file = [YKFile documentsDirectory];
[file changeDirectory:@"Assets/dir1/file1.text"];
- (void)eachFile:(void (^)(YKFile *file, NSUInteger idx))pFunction;
- (void)sortFiles:(NSComparisonResult (^)(YKFile *file1, YKFile *file2))pFunction;
- (void)removeFilesFromList:(BOOL (^)(YKFile *file))pFunction;
- (YKFileArray *)cutFilesFromList:(BOOL (^)(YKFile *file))pFunction;
+ (instancetype)homeDirectory;
+ (instancetype)mainBundleDirectory;
+ (instancetype)cachesDirectory;
+ (instancetype)documentsDirectory;
+ (instancetype)temporaryDirectory;
#pragma mark - Aliases
- (void)cd:(NSString *)path;
- (void)cd;
- (BOOL)mkdir:(NSString *)dirName;
- (BOOL)mkdirs:(NSString *)dirName;
- (BOOL)mkdir;
- (BOOL)mkdirs;
- (BOOL)rm:(NSString *)name;
- (BOOL)rm;
- (BOOL)cp:(YKFile *)destination;
- (BOOL)mv:(YKFile *)destination;
#pragma mark - 'cd' Method
- (void)changeDirectory:(NSString *)path {
NSArray *components = path.pathComponents;
for (int i = 0; i < [components count]; i++) {
NSString *component = components[i];
if (i == 0 && [component isEqualToString:@"~"]) { // cd ~
_fullPath = NSHomeDirectory();
} else if ([component isEqualToString:@".."]) { // cd ..
_fullPath = self.parentFile.fullPath;
} else if ([component isEqualToString:@"."]) { // cd .
} else if ([component isEqualToString:@"*"]) { // cd to first found file or directory
YKFileArray *list = [self listFiles];
for (YKFile *file in list) {
if ([file.name isEqual:@".DS_Store"]) continue;
if ([file.name isEqual:@"Thumbs.db"]) continue;
_fullPath = file.fullPath;
break;
}
} else { // cd component
_fullPath = [_fullPath stringByAppendingPathComponent:component];
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment