2016-06-01 8 views
9

मुझे पता है कि इस से संबंधित कुछ सवाल हैं, लेकिन वे ऑब्जेक्टिव-सी में हैं।स्विफ्ट में ऐप बंडल में शामिल फ़ाइल तक कैसे पहुंचे?

मैं कैसे उपयोग कर सकते हैं एक .txt फ़ाइल एक वास्तविक iPhone पर स्विफ्ट उपयोग करते हुए मेरे ऐप्लिकेशन में शामिल? मैं इसे पढ़ने और लिखने में सक्षम होना चाहता हूं। Here मेरी परियोजना फाइलें हैं यदि आप एक नज़र रखना चाहते हैं। यदि आवश्यक हो तो मुझे विवरण जोड़ने में खुशी होगी।

+1

"मैंने पढ़ा है और इसे से लिखने में सक्षम होना चाहता हूँ।" आप नहीं कर सकते। किसी डिवाइस पर इंस्टॉल होने पर, ऐप बंडल ** केवल पढ़ने के लिए ** है। आप अपने ऐप बंडल में एक फाइल पढ़ सकते हैं लेकिन आप इसे लिख नहीं सकते हैं। –

उत्तर

12
, बस संसाधन

var filePath = NSBundle.mainBundle().URLForResource("file", withExtension: "txt") 

हालांकि आप इसे क्योंकि यह एप्लिकेशन संसाधनों निर्देशिका में है नहीं लिख सकते हैं के लिए एप्लिकेशन बंडल में खोज के द्वारा

और आप दस्तावेज़ निर्देशिका के लिए लिखने के लिए इसे बनाने के लिए है यह

var documentsDirectory: NSURL? 
var fileURL: NSURL? 

documentsDirectory = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask).last! 
fileURL = documentsDirectory!.URLByAppendingPathComponent("file.txt") 

if (fileURL!.checkResourceIsReachableAndReturnError(nil)) { 
    print("file exist") 
}else{ 
    print("file doesnt exist") 
    NSData().writeToURL(fileURL!,atomically:true) 
} 

अब आप इसे fileURL

+0

"इसे" से आपका मतलब है कि लिखने योग्य फ़ाइल दस्तावेज़ निर्देशिका में कॉपी की गई है? – atirit

+0

"अब आप इसे fileURL से उपयोग कर सकते हैं" यह द्वारा मैं बनाई गई फ़ाइल का मतलब यह शामिल है और इसके लिखने योग्य – Karim

+0

फ़ाइल इसकी एक नया खाली फ़ाइल – Karim

1

बंडल केवल पढ़ने के लिए कर रहे हैं से पहुँच सकते हैं। आप के रूप में केवल पढ़ने के लिए फ़ाइल तक पहुँचने का NSBundle.mainBundle().pathForResource उपयोग कर सकते हैं, लेकिन पढ़ने-लिखने की पहुँच के लिए आप अपने दस्तावेज़ की प्रतिलिपि करने के दस्तावेज़ फ़ोल्डर या tmp फ़ोल्डर की जरूरत है।

8

स्विफ्ट 3, पर Karim’s answer आधारित।

let fileURL = Bundle.main.url(forResource:"filename", withExtension: "txt") 

लेखन हालांकि, अगर आप वहाँ नहीं लिख सकते हैं:

पढ़ना

आप फ़ाइलों को पढ़ सकता बंडल के संसाधन के माध्यम से किसी ऐप के बंडल में शामिल। तुम्हें पता है, एक प्रति बनाने की आवश्यकता होगी अधिमानतः दस्तावेज़ निर्देशिका में:

func makeWritableCopy(named destFileName: String, ofResourceFile originalFileName: String) throws -> URL { 
    // Get Documents directory in app bundle 
    guard let documentsDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).last else { 
     fatalError("No document directory found in application bundle.") 
    } 

    // Get URL for dest file (in Documents directory) 
    let writableFileURL = documentsDirectory.appendingPathComponent(destFileName) 

    // If dest file doesn’t exist yet 
    if (try? writableFileURL.checkResourceIsReachable()) == nil { 
     // Get original (unwritable) file’s URL 
     guard let originalFileURL = Bundle.main.url(forResource: originalFileName, withExtension: nil) else { 
      fatalError("Cannot find original file “\(originalFileName)” in application bundle’s resources.") 
     } 

     // Get original file’s contents 
     let originalContents = try Data(contentsOf: originalFileURL) 

     // Write original file’s contents to dest file 
     try originalContents.write(to: writableFileURL, options: .atomic) 
     print("Made a writable copy of file “\(originalFileName)” in “\(documentsDirectory)\\\(destFileName)”.") 

    } else { // Dest file already exists 
     // Print dest file contents 
     let contents = try String(contentsOf: writableFileURL, encoding: String.Encoding.utf8) 
     print("File “\(destFileName)” already exists in “\(documentsDirectory)”.\nContents:\n\(contents)") 
    } 

    // Return dest file URL 
    return writableFileURL 
} 

उदाहरण उपयोग:

let stuffFileURL = try makeWritableCopy(named: "Stuff.txt", ofResourceFile: "Stuff.txt") 
try "New contents".write(to: stuffFileURL, atomically: true, encoding: String.Encoding.utf8) 
3

बस स्विफ्ट 4 के साथ इस कोड का उपयोग के लिए एक त्वरित अद्यतन:

Bundle.main.url(forResource:"YourFile", withExtension: "FileExtension") 

और निम्न फ़ाइल लिखने के लिए खाते में करने के लिए अद्यतन किया गया है:

var myData: Data! 

func checkFile() { 
    if let documentsDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).last { 
     let fileURL = documentsDirectory.appendingPathComponent("YourFile.extension") 
     do { 
      let fileExists = try fileURL.checkResourceIsReachable() 
      if fileExists { 
       print("File exists") 
      } else { 
       print("File does not exist, create it") 
       writeFile(fileURL: fileURL) 
      } 
     } catch { 
      print(error.localizedDescription) 
     } 
    } 
} 

func writeFile(fileURL: URL) { 
    do { 
     try myData.write(to: fileURL) 
    } catch { 
     print(error.localizedDescription) 
    } 
} 

यह विशेष रूप से उदाहरण के सबसे लचीला नहीं है, लेकिन काम का एक छोटा सा के साथ आप आसानी से अपने खुद फ़ाइल नाम, एक्सटेंशन और डेटा मूल्यों में पारित कर सकते हैं।

संबंधित मुद्दे