2012-11-02 15 views
0

這裏的問題是我無法使用「stringByAppendingPathComponent」追加文件名稱。fma是NSFileManager。無法使用「stringByAppendingPathComponent」(Foundation)追加文件名

工作:

[fma changeCurrentDirectoryPath: [NSHomeDirectory() stringByAppendingPathComponent: @"Music/iTunes/iTunes Media/Music/ABC/DEF"]]; 
NSLog(@"Current Directory Path: %@", [fma currentDirectoryPath]); 

輸出:當前目錄路徑:/用戶/ plusa /音樂/ iTunes的/ iTunes的媒體/ 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; 
} 

fileManager.h

#import <Foundation/Foundation.h> 

@interface fileManager : NSFileManager { 
    NSArray *list; 
} 

-(int) readCurrentItem; 

@end 

fileManager.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 

回答

1

因爲你的第二個例子是一個文件,不是目錄。

假設路徑正在編寫正確,但NSFileManager不會更改文件的路徑 - 同樣,因爲它不是目錄。

+0

我該怎麼做? – PlusA

+0

@PlusA我擴大了答案 - 如果這沒有幫助,你必須在你的措辭更具體。 – justin

+0

那麼,我無法使用NSFileManager處理文件? 應該用哪種方法代替? – PlusA