2012-03-22 44 views
12

CHỈNH SỬA: SOLVED

Cảm ơn Brooks. Câu hỏi của bạn đã khiến tôi tiếp tục đào sâu nếu tập tin đó còn tồn tại trong nhóm của tôi - và nó không thành công!Sao chép thư mục (w/nội dung) từ nhóm vào thư mục Tài liệu - iOS

Vì vậy, bằng cách sử dụng mã này (bên dưới): iPhone/iPad: Không thể sao chép thư mục từ NSBundle sang NSDocumentDirectory và hướng dẫn thêm thư mục đúng cách vào Xcode (từ here trở xuống). .

Sao chép thư mục vào Xcode:

  1. Tạo một thư mục trên máy Mac của bạn.
  2. Chọn Add Files hiện tại để dự án
  3. của bạn Chọn mục bạn muốn nhập
  4. Trong cửa sổ pop-up chắc chắn rằng bạn chọn "Copy mục vào thư mục nhóm đích" và "Create Folder Tài liệu tham khảo cho bất kỳ thư mục được thêm vào "
  5. Lượt" Thêm "
  6. Thư mục sẽ xuất hiện màu xanh thay vì màu vàng.

    -(void) copyDirectory:(NSString *)directory { 
    NSFileManager *fileManager = [NSFileManager defaultManager]; 
    NSError *error; 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory = [paths objectAtIndex:0]; 
    NSString *documentDBFolderPath = [documentsDirectory stringByAppendingPathComponent:directory]; 
    NSString *resourceDBFolderPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:directory]; 
    
    if (![fileManager fileExistsAtPath:documentDBFolderPath]) { 
        //Create Directory! 
        [fileManager createDirectoryAtPath:documentDBFolderPath withIntermediateDirectories:NO attributes:nil error:&error]; 
    } else { 
        NSLog(@"Directory exists! %@", documentDBFolderPath); 
    } 
    
    NSArray *fileList = [fileManager contentsOfDirectoryAtPath:resourceDBFolderPath error:&error]; 
    for (NSString *s in fileList) { 
        NSString *newFilePath = [documentDBFolderPath stringByAppendingPathComponent:s]; 
        NSString *oldFilePath = [resourceDBFolderPath stringByAppendingPathComponent:s]; 
        if (![fileManager fileExistsAtPath:newFilePath]) { 
         //File does not exist, copy it 
         [fileManager copyItemAtPath:oldFilePath toPath:newFilePath error:&error]; 
        } else { 
         NSLog(@"File exists: %@", newFilePath); 
        } 
    } 
    

    }

======================== END EDIT

Frus-stray- shee-on! Dù sao ...

Mã bên dưới sao chép thư mục của tôi từ gói ứng dụng vào thư mục Tài liệu trong trình mô phỏng tốt. Tuy nhiên trên thiết bị tôi nhận được một lỗi và không có thư mục. Sử dụng ze Google Tôi phát hiện ra rằng lỗi (260) có nghĩa là tập tin (trong trường hợp này là thư mục của tôi) không tồn tại.

Điều gì có thể xảy ra? Tại sao tôi không thể sao chép thư mục của mình từ gói vào Tài liệu? Tôi đã kiểm tra xem các tệp có tồn tại hay không - mặc dù thư mục không hiển thị - vì Xcode muốn tệp phẳng? Nó đã biến thư mục của tôi (kéo vào Xcode) vào một tập tin phẳng của tài sản thay vào đó?

Tôi cảm ơn bạn đã hỗ trợ.

// Could not copy report at path /var/mobile/Applications/3C3D7CF6-B1F0-4561-8AD7-A367C103F4D7/cmsdemo.app/plans.gallery to path /var/mobile/Applications/3C3D7CF6-B1F0-4561-8AD7-A367C103F4D7/Documents/plans.gallery. error Error Domain=NSCocoaErrorDomain Code=260 "The operation couldn’t be completed. (Cocoa error 260.)" UserInfo=0x365090 {NSFilePath=/var/mobile/Applications/3C3D7CF6-B1F0-4561-8AD7-A367C103F4D7/cmsdemo.app/plans.gallery, NSUnderlyingError=0x365230 "The operation couldn’t be completed. No such file or directory"} 

NSString *resourceDBFolderPath; 

NSFileManager *fileManager = [NSFileManager defaultManager]; 
NSError *error; 
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, 
                NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths objectAtIndex:0]; 
NSString *documentDBFolderPath = [documentsDirectory stringByAppendingPathComponent:@"plans.gallery"]; 
BOOL success = [fileManager fileExistsAtPath:documentDBFolderPath]; 

if (success){ 
    NSLog(@"Success!"); 
    return; 
} else { 
    resourceDBFolderPath = [[[NSBundle mainBundle] resourcePath] 
             stringByAppendingPathComponent:@"plans.gallery"]; 
    [fileManager createDirectoryAtPath: documentDBFolderPath attributes:nil]; 
    //[fileManager createDirectoryAtURL:documentDBFolderPath withIntermediateDirectories:YES attributes:nil error:nil]; 

    [fileManager copyItemAtPath:resourceDBFolderPath toPath:documentDBFolderPath   
          error:&error]; 
} 

    //check if destinationFolder exists 
if ([ fileManager fileExistsAtPath:documentDBFolderPath]) 
{ 
    //removing destination, so source may be copied 
    if (![fileManager removeItemAtPath:documentDBFolderPath error:&error]) 
    { 
     NSLog(@"Could not remove old files. Error:%@",error); 
     return; 
    } 
} 
error = nil; 
//copying destination 
if (!([ fileManager copyItemAtPath:resourceDBFolderPath toPath:documentDBFolderPath error:&error ])) 
{ 
    NSLog(@"Could not copy report at path %@ to path %@. error %@",resourceDBFolderPath, documentDBFolderPath, error); 
    return ; 
} 
+1

này: 'if (([FileManager copyItemAtPath: resourceDBFolderPath toPath: Lỗi documentDBFolderPath: & lỗi ])) 'là phi logic. Bạn đang kiểm tra xem phương thức có tồn tại không, nếu không thành công. – CodaFi

Trả lời

10

Tôi đã tự do chỉnh sửa một số mã mà tôi cảm thấy cần một chút vệ sinh. Bạn đang sử dụng các phương pháp không được chấp nhận, các phương pháp quá phức tạp và chỉ đơn giản là vô lý nếu-elses. Dĩ nhiên tôi sẽ kiểm tra xem đường dẫn tệp của bạn có hợp lệ không, không biết tệp .gallery là gì, và không cố gắng cung cấp giả mạo, phán đoán duy nhất tôi có thể thực hiện là đường dẫn tệp của bạn không hợp lệ vì tài nguyên không không tồn tại nơi bạn nghĩ. (Tại một thời điểm, bạn hỏi để sao chép các tập tin vào thư mục tài liệu, sau đó kiểm tra nếu nó tồn tại trong nhóm của mình!)

-(void)testMethod { 

    NSString *resourceDBFolderPath; 

    NSFileManager *fileManager = [NSFileManager defaultManager]; 
    NSError *error; 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, 
                 NSUserDomainMask, YES); 
    NSString *documentsDirectory = [paths objectAtIndex:0]; 
    NSString *documentDBFolderPath = [documentsDirectory stringByAppendingPathComponent:@"plans.gallery"]; 
    BOOL success = [fileManager fileExistsAtPath:documentDBFolderPath]; 

    if (success){ 
     NSLog(@"Success!"); 
     return; 
    } 
    else { 
     //simplified method with more common and helpful method 
     resourceDBFolderPath = [[NSBundle mainBundle] pathForResource:@"plans" ofType:@"gallery"]; 

     //fixed a deprecated method 
     [fileManager createDirectoryAtPath:documentDBFolderPath withIntermediateDirectories:NO attributes:nil error:nil]; 

     [fileManager copyItemAtPath:resourceDBFolderPath toPath:documentDBFolderPath   
           error:&error]; 

     //check if destinationFolder exists 
     if ([ fileManager fileExistsAtPath:documentDBFolderPath]) 
     { 
      //FIXED, another method that doesn't return a boolean. check for error instead 
      if (error) 
      { 
       //NSLog first error from copyitemAtPath 
       NSLog(@"Could not remove old files. Error:%@", [error localizedDescription]); 

       //remove file path and NSLog error if it exists. 
       [fileManager removeItemAtPath:documentDBFolderPath error:&error]; 
       NSLog(@"Could not remove old files. Error:%@", [error localizedDescription]); 
       return; 
      } 
     } 
    } 
} 
+0

Đánh giá cao nhận xét/vệ sinh. – malaki1974

+1

Không sao cả! Đó là những gì tôi sống. – CodaFi

Các vấn đề liên quan