Swift set delegate to self fornece EXC_BAD_ACCESS
Estou aprendendo o Swift portando um aplicativo existente. Estou preso em definir um delegado e não consigo descobrir qual é o problema.
Eu tenho uma classe que estende 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)
}
}
Em seguida, no ViewController é definido como
class SettingsViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, SwitchCellDelegate {
e dentro do método
func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {
temos
case 2:
storeCredentialsCell = tableView.dequeueReusableCellWithIdentifier("StoreCredentialsCell") as? SwitchCell
if(storeCredentialsCell != nil){
...
NSLog("Setting delegate to %@ for %@", self.description, storeCredentialsCell.description)
storeCredentialsCell!.delegate = self
...
}
a saída do log é conforme o esperado, mas quando atinge a configuração real do delegado, o aplicativo falha com
EXC_BAD_ACCESS (código = 1, endereço = 0xfffffffffffffff8)
Também devo observar que, se eu não definir o valor delegado quando delegar? .SwitchChanged (self, state: swtSelector.on) é acionado, isso também causa um erro EXC_BAD_ACCESS, mas, de acordo com o documento para delegados, isso deve falhar normalmente se o delegado não for definido para qualquer coisa.
===========================
Simplifiquei um projeto básico para replicar o problema.
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)
}
}
Em seguida, criei uma única cena do controlador de exibição de tabela com a classe TestTableViewController. A exibição da tabela é dinâmica com uma única célula do tipo TestCell. Essa célula contém um rótulo e uma opção que estão vinculados aos IBOutlets da classe TestCell. A função switchChanged está vinculada ao evento de valor alterado no comutador.
O mesmo erro EXC_BAD_ACCESS é lançado.