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:
- Tạo một thư mục trên máy Mac của bạn.
- Chọn Add Files hiện tại để dự án
- của bạn Chọn mục bạn muốn nhập
- 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 "
- Lượt" Thêm "
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 ;
}
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