0

这里的问题是我无法使用“stringByAppendingPathComponent”附加文件名 fma 是 NSFileManager。

在职的:

[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

更新

我要做的是创建一个简单的文件管理器。

主文件

#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;
}

文件管理器.h

#import <Foundation/Foundation.h>

@interface fileManager : NSFileManager {
    NSArray *list;
}

-(int) readCurrentItem;

@end

文件管理器.m

#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
4

1 回答 1

1

因为您的第二个示例指的是文件,而不是目录。

据推测,路径正在正确组合,但NSFileManager并没有更改文件的路径 - 再次,因为它不是目录。

于 2012-11-02T08:59:26.570 回答