संपादित करें: सोल्व
धन्यवाद ब्रूक्स। आपके प्रश्न ने मुझे खोदने के लिए प्रेरित किया अगर फाइल मेरे बंडल में भी मौजूद थी - और ऐसा नहीं हुआ!बंडल से दस्तावेज़ निर्देशिका में फ़ोल्डर (डब्ल्यू/सामग्री) कॉपी करें - आईओएस
तो यह कोड (साथ ही नीचे) का उपयोग करके: iPhone/iPad: (here और नीचे से) Xcode करने के लिए ठीक से एक निर्देशिका जोड़ने मैं यह काम करने के लिए प्राप्त करने में सक्षम था के लिए NSDocumentDirectory और निर्देशों के NSBundle से फ़ोल्डर कॉपी करने में असमर्थ । Xcode में
कॉपी फ़ोल्डर:
- अपने मैक पर एक निर्देशिका बनाएँ।
- चुनें अपनी परियोजना
- को मौजूदा फ़ाइलों जोड़े निर्देशिका आप आयात करना
- पॉप-अप विंडो में सुनिश्चित करें कि आप " गंतव्य समूह के फ़ोल्डर में कॉपी आइटम करें" और "किसी भी के लिए फ़ोल्डर संदर्भ बनाएं बनाना चाहते हैं उसे चुनें जोड़ा फ़ोल्डर्स "
- " जोड़ें "
निर्देशिका पीले रंग की बजाय नीली दिखाई देनी चाहिए।
-(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); } }
}
======================== अंत संपादित
Frus-stray- shee-ऑन! वैसे भी ...
नीचे दिया गया कोड मेरे फ़ोल्डर को ऐप बंडल से सिम्युलेटर में दस्तावेज़ फ़ोल्डर में कॉपी करता है ठीक है। हालांकि डिवाइस पर मुझे एक त्रुटि और कोई फ़ोल्डर नहीं मिलता है। ज़ी Google का उपयोग करके मुझे पता चला कि त्रुटि (260) का अर्थ है फ़ाइल (इस मामले में मेरा फ़ोल्डर) मौजूद नहीं है।
क्या गलत हो सकता है? मैं अपने फ़ोल्डर को बंडल से दस्तावेज़ों में क्यों कॉपी नहीं कर सकता? मैंने जांच की है कि फाइल मौजूद हैं - हालांकि फ़ोल्डर दिखाई नहीं दे रहा है - क्योंकि एक्सकोड फ्लैट फ़ाइल चाहता है? क्या यह मेरे फ़ोल्डर (एक्सकोड में खींच लिया गया) बदले में संपत्ति की एक फ्लैट फ़ाइल में बदल गया?
मैं किसी भी सहायता के लिए धन्यवाद।
// 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 ;
}
इस: 'अगर (([filemanager copyItemAtPath: resourceDBFolderPath toPath: documentDBFolderPath त्रुटि: और त्रुटि ])) 'अजीब है। आप जांच रहे हैं कि विधि मौजूद है या नहीं, अगर यह सफल हुआ। – CodaFi