2012-08-06 68 views
0

我正在閱讀的一章是關於NSFileManager,作者說要創建一個名爲testFile的空文件。 testFilemain.m位於同一文件夾下。我沒有創建newfile。我無法複製testFile,它的returning 2NSLog@"couldnt copy file"。我試圖把toPath的論點:作爲@"/Users/el/Desktop/prog/prog/newfile"copyItemAtPath:toPath:error:is NO NO

int main (int argc, char *argv[]) { 
    @autoreleasepool { 
     NSString *fName = @"/Users/el/Desktop/prog/prog/testFile"; 
     NSFileManager *fm = [NSFileManager defaultManager]; 

     if ([fm fileExistsAtPath: fName] == NO) { 
      NSLog(@"couldnt find file"); 
      return 1; 
     } 

     if ([fm copyItemAtPath:fName toPath:@"newfile" error:NULL] == NO) { 
      NSLog(@"couldnt copy file"); 
      return 2; 
     } 

回答

0

第二個路徑也必須是絕對的。

[fm copyItemAtPath: @"/Users/el/Desktop/prog/prog/testFile" toPath: @"/Users/el/Desktop/prog/prog/newFile" error: NULL]; 

注意:您應該使用NSURL爲基礎的方法,而不是基於NSString那些。

+0

對不起,'[fm copyItemAtPath:@「/ Users/el/Desktop/prog/prog/testFile」toPath:@「/ Users/el/Desktop/prog/prog/newfile」error:NULL]'did not你有什麼建議嗎?哦,我只是遵循作者指示的內容。也許他會稍後再過「NSURL」。 – Emily 2012-08-06 17:49:50

+0

@Emily你是否確定這條道路存在,你有權寫入它?我不知道爲什麼它會失敗。如果你嘗試使用URL變體:[[fm copyItemAtURL:[NSURL fileURLWithPath:@「/ Users/el/Desktop/prog/prog/testFile」] toURL:[NSURL fileURLWithPath:@「/ Users/el/Desktop/prog /編/新文件「]錯誤:無]'? – 2012-08-06 18:03:34

+0

@Emily你也可以做的其實是要求錯誤信息,如下所示:'NSError * error = nil; [fm copyItemAtURL:(...)toURL:(...)error:&error];如果(錯誤)NSLog(錯誤);' – 2012-08-06 18:06:15