ios - swift: Writing to a file -
another swift question today. i'm trying publish users name , score textfile compile highscore list. using writetofile() function , getting no errors, in breakpoints line being reached , executed ok still see no changed reflected in score.txt file. should mention score.txt included in build path project...not sure if changes anything, thought should mention it. here code:
import foundation import uikit class victoryviewcontroller : uiviewcontroller { // define outputs @iboutlet weak var submitscorebtn: uibutton! @iboutlet weak var scorename: uitextfield! // define string hold score value var score = string() // // begin functions // override func viewdidload() { super.viewdidload() submitscorebtn.addtarget(self, action: "submitscore:", forcontrolevents: uicontrolevents.touchupinside) print(score) } //submit score db func submitscore(sender: uibutton! ) { var text: string = scorename.text! text += "," text += score print(text) if let dir : nsstring = nssearchpathfordirectoriesindomains(nssearchpathdirectory.documentdirectory, nssearchpathdomainmask.alldomainsmask, true).first { let path = dir.stringbyappendingpathcomponent("scores.txt"); //writing { try text.writetofile(path, atomically: false, encoding: nsutf8stringencoding) } catch {print("error writing score.")} } } }
make sure looking @ proper file.
the file @ in xcode on mac, in project directory. not same file simulator or ios device writes to.
finding right file xcode bit complicated: open "devices" view, select device, select app, open cogwheel popup , select "download container..." , save locally. can inspect contents of documents directory in downloaded container , check files contents xcode.
Comments
Post a Comment