Swift set delegate to self gives EXC_BAD_ACCESS -
i'm going through , learning swift porting existing application. i'm stuck on setting delegate , cannot work out issue is.
i have class extends uitableviewcell
import uikit protocol switchcelldelegate{ func switchchanged(switchcell: switchcell, state: bool) } class switchcell: uitableviewcell { @iboutlet var swtselector: uiswitch @iboutlet var lbltitle: uilabel var delegate: switchcelldelegate? init(style: uitableviewcellstyle, reuseidentifier: string) { super.init(style: style, reuseidentifier: reuseidentifier) } @ibaction func switchchanged(){ delegate?.switchchanged(self, state: swtselector.on) } }
then in viewcontroller defined
class settingsviewcontroller: uiviewcontroller, uitableviewdelegate, uitableviewdatasource, switchcelldelegate {
and within method
func tableview(tableview: uitableview!, cellforrowatindexpath indexpath: nsindexpath!) -> uitableviewcell! {
we have
case 2: storecredentialscell = tableview.dequeuereusablecellwithidentifier("storecredentialscell") as? switchcell if(storecredentialscell != nil){ ... nslog("setting delegate %@ %@", self.description, storecredentialscell.description) storecredentialscell!.delegate = self ... }
the log output expected when hits actual setting of delegate app crashes with
exc_bad_access(code=1, address=0xfffffffffffffff8)
i should note if don't set delegate value when delegate?.switchchanged(self, state: swtselector.on) fires causes exc_bad_access error according doco delegates should fail gracefully if delegate not set anything.
===========================
i've simplified down basic project replicate issue.
testtableviewcontroller.swift
import uikit class testtableviewcontroller: uitableviewcontroller, testcelldelegate { init(style: uitableviewstyle) { super.init(style: style) } init(coder adecoder: nscoder!) { super.init(coder: adecoder) } override func viewdidload() { super.viewdidload() } override func didreceivememorywarning() { super.didreceivememorywarning() } override func numberofsectionsintableview(tableview: uitableview?) -> int { return 1 } override func tableview(tableview: uitableview?, numberofrowsinsection section: int) -> int { return 1 } override func tableview(tableview: uitableview?, cellforrowatindexpath indexpath: nsindexpath?) -> uitableviewcell? { let cell = tableview!.dequeuereusablecellwithidentifier("cell", forindexpath: indexpath) as? testcell if(cell != nil){ cell!.delegate = self cell!.lbltest.text = "test successful" } return cell } func eventfired(sender: testcell) { nslog("hooray!") }
testcell.swift
import uikit protocol testcelldelegate{ func eventfired(sender: testcell) } class testcell: uitableviewcell { @iboutlet var lbltest: uilabel @iboutlet var swttest: uiswitch var delegate: testcelldelegate? init(style: uitableviewcellstyle, reuseidentifier: string) { super.init(style: style, reuseidentifier: reuseidentifier) } @ibaction func switchchanged(sender: uiswitch){ delegate?.eventfired(self) } }
i created single table view controller scene class of testtableviewcontroller. table view dynamic single cell of type testcell. cell contains 1 label , 1 switch bound iboutlets testcell class. switchchanged function bound value changed event on switch.
same exc_bad_access error thrown.
currently have explicitly mark protocols @objc
if delegate should object of objective-c class (like uitableviewcontroller):
@objc protocol swiftprotocol
this enable interoperating objective-c
Comments
Post a Comment