Xcode 无法使用“添加文件名”;stringByAppendingPathComponent";(基金会)
这里的问题是,我无法使用“stringByAppendingPathComponent”追加文件名 fma是一个NSFileManager 工作:Xcode 无法使用“添加文件名”;stringByAppendingPathComponent";(基金会),xcode,nsstring,filesystems,nsfilemanager,foundation,Xcode,Nsstring,Filesystems,Nsfilemanager,Foundation,这里的问题是,我无法使用“stringByAppendingPathComponent”追加文件名 fma是一个NSFileManager 工作: [fma changeCurrentDirectoryPath: [NSHomeDirectory() stringByAppendingPathComponent: @"Music/iTunes/iTunes Media/Music/ABC/DEF"]]; NSLog(@"Current Directory Path: %@", [fma curr
[fma changeCurrentDirectoryPath: [NSHomeDirectory() stringByAppendingPathComponent: @"Music/iTunes/iTunes Media/Music/ABC/DEF"]];
NSLog(@"Current Directory Path: %@", [fma currentDirectoryPath]);
输出:当前目录路径:/Users/plusa/Music/iTunes/iTunes Media/ABC/DEF
不工作:
[fma changeCurrentDirectoryPath: [NSHomeDirectory() stringByAppendingPathComponent: @"Music/iTunes/iTunes Media/Music/ABC/DEF/a.mp3"]];
NSLog(@"Current Directory Path: %@", [fma currentDirectoryPath]); // No change
更新 我要做的是创建一个简单的文件管理器 main.m
#import <Foundation/Foundation.h>
#import "fileManager.h"
int main(int argc, const char * argv[])
{
@autoreleasepool {
NSLog(@"Welcome to File Manager.");
fileManager *fma = [[fileManager alloc] init];
NSLog(@"Current Directory Path: %@", [fma currentDirectoryPath]);
[fma changeCurrentDirectoryPath: [NSHomeDirectory() stringByAppendingPathComponent: @"Music/iTunes/iTunes Media/Music"]];
NSLog(@"Directory Path changed to iTunes Music source base");
[fma readCurrentItem];
}
return 0;
}
因为您的第二个示例引用的是文件,而不是目录
据推测,路径的组成是正确的,但是
NSFileManager
没有更改文件的路径——同样,因为它不是一个目录。@PlusA我扩展了答案——如果这没有帮助,你必须在措辞上更加具体。那么,我无法使用NSFileManager处理文件?应该使用哪种方法来代替?@PlusA您当然可以,但我认为您可能误解了“当前目录路径”的用途(方便引用相对于指定目录的路径)。但是你想如何“处理”一个文件呢?对不起,我的英语太差了!我正在创建一个简单的文件管理器,用于浏览目录,让用户选择要读取的目录,我使用它来获取路径。如果是文件,请使用NSData,但即使是我也无法将“指针”添加到NSFileManager以指向文件路径。我现在扩展了这个问题。@PlusA您可以使用fileExistsAtPath:isDirectory:
而不是属性。
#import <Foundation/Foundation.h>
@interface fileManager : NSFileManager {
NSArray *list;
}
-(int) readCurrentItem;
@end
#import "fileManager.h"
@implementation fileManager
-(int) readCurrentItem
{
int i = 1;
NSLog(@"Current Directory Path: %@", [self currentDirectoryPath]);
NSLog(@"Reading Directory Path...");
if(NSFileTypeDirectory == [[self attributesOfItemAtPath: [self currentDirectoryPath] error: nil] objectForKey: @"NSFileType"]) {
list = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:[self currentDirectoryPath] error: nil];
} else {
NSLog(@"File");
return 0;
}
i = 1;
for (NSString *item in list)
NSLog(@"Item #%i: %@", i++, item);
NSLog(@"The item to read:");
scanf("%i", &i);
if (i == 0) {
NSLog(@"Shutdown.");
return 0;
}
[self changeCurrentDirectoryPath: [[self currentDirectoryPath] stringByAppendingPathComponent: [list[(i - 1)] lastPathComponent]]];
[self readCurrentItem];
}
@end