2012-03-22 18 views
12

संपादित करें: सोल्व

धन्यवाद ब्रूक्स। आपके प्रश्न ने मुझे खोदने के लिए प्रेरित किया अगर फाइल मेरे बंडल में भी मौजूद थी - और ऐसा नहीं हुआ!बंडल से दस्तावेज़ निर्देशिका में फ़ोल्डर (डब्ल्यू/सामग्री) कॉपी करें - आईओएस

तो यह कोड (साथ ही नीचे) का उपयोग करके: iPhone/iPad: (here और नीचे से) Xcode करने के लिए ठीक से एक निर्देशिका जोड़ने मैं यह काम करने के लिए प्राप्त करने में सक्षम था के लिए NSDocumentDirectory और निर्देशों के NSBundle से फ़ोल्डर कॉपी करने में असमर्थ । Xcode में

कॉपी फ़ोल्डर:

  1. अपने मैक पर एक निर्देशिका बनाएँ।
  2. चुनें अपनी परियोजना
  3. को मौजूदा फ़ाइलों जोड़े निर्देशिका आप आयात करना
  4. पॉप-अप विंडो में सुनिश्चित करें कि आप " गंतव्य समूह के फ़ोल्डर में कॉपी आइटम करें" और "किसी भी के लिए फ़ोल्डर संदर्भ बनाएं बनाना चाहते हैं उसे चुनें जोड़ा फ़ोल्डर्स "
  5. " जोड़ें "
  6. निर्देशिका पीले रंग की बजाय नीली दिखाई देनी चाहिए।

    -(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 ; 
} 
+1

इस: 'अगर (([filemanager copyItemAtPath: resourceDBFolderPath toPath: documentDBFolderPath त्रुटि: और त्रुटि ])) 'अजीब है। आप जांच रहे हैं कि विधि मौजूद है या नहीं, अगर यह सफल हुआ। – CodaFi

उत्तर

10

मैंने कुछ कोड संपादित करने की स्वतंत्रता ली है, मुझे लगता है कि मुझे थोड़ा हाउसकीपिंग चाहिए। आप बहिष्कृत विधियों, अत्यधिक जटिल तरीकों का उपयोग कर रहे हैं, और केवल सादे हास्यास्पद if-elses का उपयोग कर रहे हैं। मैं निश्चित रूप से जांचता हूं कि आपका फ़ाइल पथ मान्य है, यह नहीं पता कि एक। गैलरी फ़ाइल क्या है, और एक डमी प्रदान करने का कोई प्रयास नहीं कर रहा है, केवल एक ही निर्णय मैं कर सकता हूं कि आपका फ़ाइल पथ बस अमान्य है क्योंकि संसाधन नहीं करता है अस्तित्व में नहीं है जहां आप सोचते हैं कि यह करता है। (एक बिंदु पर, आप दस्तावेज़ों निर्देशिका में फाइल कॉपी करने के लिए पूछते हैं, तो जांचें कि यह आपकी बंडल में मौजूद है!)

-(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

टिप्पणियों/हाउसकीपिंग की सराहना करें। – malaki1974

+1

कोई समस्या नहीं! मैं यही रहता हूं। – CodaFi