ios - Cannot invoke 'append' with an argument list of type'(String)' in my firstViewController -
firstviewcontroller. problem is. in 'for loop' giving me error. have tried use xcode suggested, isn't working. still consider myself beginner swift. looked @ other answers when people have posted same question , still can't solve problem.
import uikit var todoitems:[string] = [] class firstviewcontroller: uiviewcontroller, uitableviewdatasource, uitableviewdelegate { @iboutlet var taskstable:uitableview? override func viewdidload() { super.viewdidload() } func tableview(tableview: uitableview, numberofrowsinsection section: int) -> int { return todoitems.count } func tableview(tableview: uitableview, cellforrowatindexpath indexpath: nsindexpath) -> uitableviewcell { var cell = uitableviewcell(style: uitableviewcellstyle.default, reuseidentifier: "cell") cell.textlabel?.text = todoitems[indexpath.row] return cell } override func didreceivememorywarning() { super.didreceivememorywarning() // dispose of resources can recreated. } override func viewwillappear(animated: bool) { if var storedtodoitems : anyobject? = nsuserdefaults.standarduserdefaults().objectforkey("todoitems") { todoitems = [] var = 0; < storedtodoitems?.count(); ++i { todoitems.append(storedtodoitems[i] as! string) } } taskstable?.reloaddata() } }
this secondviewcontroller.
import uikit class secondviewcontroller: uiviewcontroller, uitextfielddelegate { @iboutlet weak var todoitem: uitextfield! @ibaction func additembutton(sender: anyobject) { todoitems.append(todoitem.text) let fixedtodoitems = todoitems nsuserdefaults.standarduserdefaults().setobject(fixedtodoitems, forkey: "toddoitems") nsuserdefaults.standarduserdefaults().synchronize() self.view.endediting(true) } override func viewdidload() { super.viewdidload() // additional setup after loading view, typically nib. } override func didreceivememorywarning() { super.didreceivememorywarning() // dispose of resources can recreated. } func textfieldshouldreturn(textfield: uitextfield) -> bool { textfield.resignfirstresponder() return true } override func touchesbegan(touches: set<nsobject>, withevent event: uievent) { self.view.endediting(true) } }
when load to-do list array nsuserdefaults
, use if let
test if it's [string]
(that is, array of strings), , won't need cast string
later on, might contributing error. try replacing viewwillappear
method this:
override func viewwillappear(animated: bool) { if let storedtodoitems = nsuserdefaults.standarduserdefaults().objectforkey("todoitems") as? [string] { todoitems = [] var = 0; < storedtodoitems.count; ++i { todoitems.append(storedtodoitems[i]) } } taskstable?.reloaddata() }
while you're @ it, there's nicer way loop on contents of array in swift (and many other languages):
override func viewwillappear(animated: bool) { if let storedtodoitems = nsuserdefaults.standarduserdefaults().objectforkey("todoitems") as? [string] { todoitems = [] item in storedtodoitems { todoitems.append(item) } } taskstable?.reloaddata() }
in case however, should able set variable directly loaded one:
override func viewwillappear(animated: bool) { if let storedtodoitems = nsuserdefaults.standarduserdefaults().objectforkey("todoitems") as? [string] { todoitems = storedtodoitems } taskstable?.reloaddata() }
Comments
Post a Comment