swift - Read and write data from text file -


i need read , write data to/from text file, haven't been able figure out how.

i found sample code in swift's ibook, still don't know how write or read data.

import cocoa  class dataimporter {     /*     dataimporter class import data external file.     class assumed take non-trivial amount of time initialize.     */     var filename = "data.txt"     // dataimporter class provide data importing functionality here }  class datamanager {     @lazy var importer = dataimporter()     var data = string[]()     // datamanager class provide data management functionality here }  let manager = datamanager() manager.data += "some data" manager.data += "some more data" // dataimporter instance importer property has not yet been created”  println(manager.importer.filename) // dataimporter instance importer property has been created // prints "data.txt”    var str = "hello world in swift language." 

for reading , writing should use location writeable, example documents directory. following code shows how read , write simple string. can test on playground.

swift 1.x

let file = "file.txt"  if let dirs : [string] = nssearchpathfordirectoriesindomains(nssearchpathdirectory.documentdirectory, nssearchpathdomainmask.alldomainsmask, true) as? [string] {     let dir = dirs[0] //documents directory     let path = dir.stringbyappendingpathcomponent(file);     let text = "some text"      //writing     text.writetofile(path, atomically: false, encoding: nsutf8stringencoding, error: nil);      //reading     let text2 = string(contentsoffile: path, encoding: nsutf8stringencoding, error: nil) } 

swift 2.2

let file = "file.txt" //this file. write , read  let text = "some text" //just text  if let dir = nssearchpathfordirectoriesindomains(nssearchpathdirectory.documentdirectory, nssearchpathdomainmask.alldomainsmask, true).first {     let path = nsurl(fileurlwithpath: dir).urlbyappendingpathcomponent(file)      //writing     {         try text.writetourl(path, atomically: false, encoding: nsutf8stringencoding)     }     catch {/* error handling here */}      //reading     {         let text2 = try nsstring(contentsofurl: path, encoding: nsutf8stringencoding)     }     catch {/* error handling here */} } 

swift 3.0

let file = "file.txt" //this file. write , read  let text = "some text" //just text  if let dir = filemanager.default.urls(for: .documentdirectory, in: .userdomainmask).first {      let path = dir.appendingpathcomponent(file)      //writing     {         try text.write(to: path, atomically: false, encoding: string.encoding.utf8)     }     catch {/* error handling here */}      //reading     {         let text2 = try string(contentsof: path, encoding: string.encoding.utf8)     }     catch {/* error handling here */} } 

Comments

Popular posts from this blog

python - TypeError: start must be a integer -

c# - DevExpress RepositoryItemComboBox BackColor property ignored -

django - Creating multiple model instances in DRF3 -