我在iOS應用程序中繼承NSFileManager以添加一些其他行爲。雖然這樣做,我遇到了一個問題,我可以演示沒有子類化。NSFileManager實例更改其他實例的當前目錄路徑
總之,我創建的NSFileManager的一個實例,改變其currentDirectoryPath,打印出來,然後創建另一個實例,設置其currentDirectoryPath別的東西,我結束了兩個相同的currentDirectoryPath實例。
這是如何複製此行爲:
NSString * pathA = [NSHomeDirectory() stringByAppendingPathComponent:@"directoryA"];
NSString * pathB = [NSHomeDirectory() stringByAppendingPathComponent:@"directoryB"];
[[NSFileManager defaultManager] createDirectoryAtPath:pathA withIntermediateDirectories:NO attributes:nil error:nil];
[[NSFileManager defaultManager] createDirectoryAtPath:pathB withIntermediateDirectories:NO attributes:nil error:nil];
NSFileManager * fileManagerA = [[CCFileManager alloc] init];
[fileManagerA changeCurrentDirectoryPath:pathA];
NSLog(@"fileManagerA.currentDirectoryPath = %@", fileManagerA.currentDirectoryPath); // outputs pathA, as expected
NSFileManager * fileManagerB = [[CCFileManager alloc] init];
[fileManagerB changeCurrentDirectoryPath:pathB];
NSLog(@"fileManagerB.currentDirectoryPath = %@", fileManagerB.currentDirectoryPath); // outputs pathB, as expected
NSLog(@"fileManagerA.currentDirectoryPath = %@", fileManagerA.currentDirectoryPath); // outputs pathB, why?
我都試過SDK iOS上的7 7.1和8.1和8相同的結果。
的實例[的NSFileManager defaultManager]是一個單。 – danh 2014-11-06 03:31:32
@danh不是defaultManager的單身人士嗎? – SuitedSloth 2014-11-06 03:33:52
@danh我認爲問題在於changeCurrentDirectoryPath更改整個過程的設置,而不僅僅是該實例。 – SuitedSloth 2014-11-06 03:36:05