Adding rows to a swift array of my Schedule from a Detail View Controller won't work -
i have list of events each of them pass in data detail vc object core data model. example, event
can have title, date, room, isfavorite, etc. now, detailvc
, have "add schedule" button let user select favorite event attend. once clicked, save object array.
i error1:
cannot invoke append argument list of type string.
detail view controller:
class detailviewcontroller: uiviewcontroller, uitableviewdatasource, uitableviewdelegate { var managedobjectcontext: nsmanagedobjectcontext? = nil // @iboutlet weak var detaildescriptionlabel: uilabel! @iboutlet weak var tableview: uitableview! //create new array hold on favorite objects (sessions add schedule) var favesarray = [event]() var detailitem: event!
this detail view controller cellforrowatindexpath
:
func tableview(tableview: uitableview, cellforrowatindexpath indexpath: nsindexpath) -> uitableviewcell { if indexpath.section == sections.info && indexpath.row == 1 { let cell: titlecell = tableview.dequeuereusablecellwithidentifier("favecell", forindexpath: indexpath) as! titlecell if detailitem.isfavorite.boolvalue { cell.valuefavebutton.settitle("remove schedule", forstate: .normal) } else { cell.valuefavebutton.settitle("add schedule", forstate: .normal) } cell.valuefavebutton.addtarget(self, action: "myschedulebutton:", forcontrolevents: .touchupinside) return cell } else if indexpath.section == sections.info && indexpath.row == 0 { let cell: titlecell = tableview.dequeuereusablecellwithidentifier("titlecell", forindexpath: indexpath) as! titlecell let dateformatter = nsdateformatter() dateformatter.dateformat = "hh:mm" var datestring = dateformatter.stringfromdate(detailitem!.date) cell.titlelabel.text = detailitem?.title cell.fieldlabel.text = datestring return cell } else if indexpath.section == sections.info && indexpath.row == 2 { let cell: removefavecell = tableview.dequeuereusablecellwithidentifier("removefavecell", forindexpath: indexpath) as! removefavecell cell.removefavebutton.addtarget(self, action: "removefavoritebutton:", forcontrolevents: .touchupinside) return cell } else if indexpath.section == sections.description { let cell: labelcell = tableview.dequeuereusablecellwithidentifier("labelcell", forindexpath: indexpath) as! labelcell cell.labeldescriptionfield.text = "swift new language apple." return cell } else { assertionfailure("unhandled session table view section") let cell = tableview.dequeuereusablecellwithidentifier("reuseidentifier", forindexpath: indexpath) as! uitableviewcell return cell } } func myschedulebutton(sender: uibutton) { detailitem.isfavorite = !detailitem.isfavorite.boolvalue tableview.reloadsections(nsindexset(index: sections.info), withrowanimation: .automatic) //now add clicked/save object(event row) on faves array //1 yellow warning in next line:conditional cast event event succeeds. compiles fine. if let favesession:event = detailitem as? event { self.favesarray.append(favesession) } }
can't figure out yet. valid approach work core data object saved later new arrays? in case, shown when segment button called schedule pressed. thank answers.
event.swift
class event: nsmanagedobject { @nsmanaged var date: nsdate @nsmanaged var isfavorite: nsnumber @nsmanaged var timestamp: nsdate @nsmanaged var title: string @nsmanaged var classselected: string @nsmanaged var desc: string }
display schedule (favesarray objects selected in detailvc)
@ibaction func filterhsclassesbyperiod(sender: uisegmentedcontrol) { let classselected: string? if sender.selectedsegmentindex == 0 { classselected = "period 1" }else { classselected = "my schedule" } let filterpredicate = nspredicate(format: "classselected = %@", classselected!) var request: nsfetchrequest = self.fetchedresultscontroller.fetchrequest request.predicate = filterpredicate var e: nserror? = nil self.fetchedresultscontroller.performfetch(&e) self.tableview.reloaddata() }
you define favesarray array of event:
var favesarray = [event]()
but trying append string
if let favesession:string = detailitem.title as? string { self.favesarray.append(favesession) }
if want able append event , string need change declaration be:
var favesarray = [anyobject]()
or (if possible) cast favesession event instead string, can decide if possible
if let favesession:event = detailitem.title as? event { self.favesarray.append(favesession) }
Comments
Post a Comment