2010-01-31 15 views
7
BOOL success; 
NSFileManager *fileManager = [[NSFileManager defaultManager]autorelease]; 
NSError *error; 
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, 
                 NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths objectAtIndex:0]; 
NSString *documentDBFolderPath = [documentsDirectory stringByAppendingPathComponent:@"DB"]; 
success = [fileManager fileExistsAtPath:documentDBFolderPath]; 

if (success){ 
return; 
}else{ 
NSString *resourceDBFolderPath = [[[NSBundle mainBundle] resourcePath] 
             stringByAppendingPathComponent:@"DB"]; 
[fileManager createDirectoryAtPath: documentDBFolderPath attributes:nil]; 
    [fileManager copyItemAtPath:resourceDBFolderPath toPath:documentDBFolderPath   
                     error:&error]; 
} 
} 

इस तरह।आईफोन संसाधन निर्देशिका से दस्तावेज़ निर्देशिका में कॉपी करें

संसाधन/DB/words.csv => डीबी फ़ोल्डर प्रतिलिपि => दस्तावेज़/DB/words.csv

मैं संसाधन फ़ोल्डर में डीबी उपनिर्देशिका प्रतिलिपि बनाना चाहते हैं। मैंने सोचा कि स्रोत अच्छा है। लेकिन वह स्रोत फ़ोल्डर बनाता है और संसाधन फ़ोल्डर में डीबी फ़ोल्डर में फ़ाइलों की प्रतिलिपि नहीं बनाता है।

मैं वास्तव में संसाधन फ़ोल्डर में फ़ाइलों को डीबी फ़ोल्डर में कॉपी करना चाहता हूं। क्रिप्या मेरि सहायता करे।

उत्तर

8

1) NSFileManager नहीं है। आप इसे डबल-रिलीज़ कर रहे हैं जो आपके ऐप को क्रैश करेगा।

2) -createDirectoryAtPath: पर कॉल करने की आवश्यकता नहीं है। -copyItemAtPath:toPath:error: की एसडीके दस्तावेज़, से

srcPath मौजूद होना चाहिए में निर्दिष्ट फ़ाइल

, जबकि dstPath आपरेशन

और विफल निर्देशिका प्रतिलिपि बनाने से पहले अस्तित्व में नहीं होना चाहिए ।

+0

ओह का उपयोग करना !! वास्तव में वास्तव में वास्तव में धन्यवाद !! धन्यवाद!! – Beomseok

1

स्विफ्ट 3,0

स्ट्रिंग

func copyFolder(){ 

    // Get the resource folder 
    if let resourceMainPath = Bundle.main.resourcePath{ 

     var isDirectory = ObjCBool(true) 
     // Get the path of the folder to copy 
     let originPath = (resourceMainPath as NSString).appendingPathComponent("NameOfFolder") 
     // Get the destination path, here copying to Caches 
     let destinationPath = NSSearchPathForDirectoriesInDomains(.cachesDirectory, .userDomainMask, true).first! 
     // Append the folder name to dest path so that system creates the directory if it doesnt exist 
     let destPath = (destinationPath as NSString).appendingPathComponent("/NameOfFolder") 
     let fileManager = FileManager.default 
     if fileManager.fileExists(atPath: destPath, isDirectory:&isDirectory){ 
      // If an overwrite behavior is needed, remove and copy again here 
      print("Exists") 
     }else{ 
      // Do the copy 
      do { 
       try fileManager.copyItem(atPath: originPath, toPath: destPath) 
      }catch let error{ 
       print(error.localizedDescription) 
      } 
     } 
    }else{ 

    } 

} 

का उपयोग यूआरएल

func copyTheFolder(){ 

    // Get the resource folder 
    if let resourceMainURL = Bundle.main.resourceURL{ 
     var isDirectory = ObjCBool(true) 
     // Get the path of the folder to copy 
     let originPath = resourceMainURL.appendingPathComponent("NameOfFolder") 
     // Get the destination path, here copying to Caches 
     let destinationPath = NSSearchPathForDirectoriesInDomains(.cachesDirectory, .userDomainMask, true).first! 
     // Append the folder name to dest path so that system creates the directory if it doesnt exist 
     let destURL = URL(fileURLWithPath: destinationPath).appendingPathComponent("/NameOfFolder") 
     let fileManager = FileManager.default 
     if fileManager.fileExists(atPath: destURL.path, isDirectory:&isDirectory){ 
      // If an overwrite behavior is needed, remove and copy again here 
      print("Exists") 

     }else{ 
      // Do the copy 
      do { 
       try fileManager.copyItem(at: originPath, to: destURL) 

      }catch let error{ 
       print(error.localizedDescription) 
      } 
     } 
    } 
} 
संबंधित मुद्दे