ios - writetofile always returns false -


i trying write content file in swift

let jsonresult = try!   nsjsonserialization.jsonobjectwithdata(response.data, options:   nsjsonreadingoptions.mutablecontainers) as! nsdictionary     let filemanager = nsfilemanager.defaultmanager() let urls = filemanager.urlsfordirectory(.documentdirectory, indomains: .userdomainmask)  let documentdirectory = urls.first!.path let path = nsurl(fileurlwithpath: documentdirectory!).urlbyappendingpathcomponent("data.txt") let dict = jsonresult nsdictionary let status = dict.writetofile(path.path!, atomically: false) print(status) 

but content not written file , status false

swift 2

        let file = "file.txt" //this file. write , read          let jsonresult : nsmutabledictionary = nsmutabledictionary.init(object: "08:00:00", forkey: "start_hour");         jsonresult.setvalue("10:00:00", forkey: "end_hour");         jsonresult.setvalue("30", forkey: "call_duration");          let dict = jsonresult nsdictionary          if let dir : nsstring = nssearchpathfordirectoriesindomains(nssearchpathdirectory.documentdirectory, nssearchpathdomainmask.alldomainsmask, true).first {             let path = nsurl(fileurlwithpath: dir string).urlbyappendingpathcomponent(file);              //writing             dict.writetofile(path.path!, atomically: false)              var contents : nsstring             //reading             {                 contents = try nsstring(contentsoffile: path.path!, encoding: nsutf8stringencoding)             }             catch {                 /* error handling here */                 contents  = ""             }             print(contents string);         } 

if have file in bundle, use following code find path , read file.

    let path = nsbundle.mainbundle().pathforresource("sample-text", oftype: "txt")      let contents: nsstring     {         contents = try nsstring(contentsoffile: path!, encoding: nsutf8stringencoding)     } catch _ {         contents = ""     } 

parse web service response :

        // responsedata - api response data.          // parse result json, since that's api provides         let getconfig: nsdictionary         {              getconfig = try nsjsonserialization.jsonobjectwithdata(responsedata, options: nsjsonreadingoptions()) as! nsdictionary         } catch  {             print("error trying convert data json")         } 

Comments

Popular posts from this blog

java - Run spring boot application error: Cannot instantiate interface org.springframework.context.ApplicationListener -

python - pip wont install .WHL files -

Excel VBA "Microsoft Windows Common Controls 6.0 (SP6)" Location Changes -