Swift establece delegado a sí mismo da EXC_BAD_ACCESS
Estoy revisando y aprendiendo Swift al portar una aplicación existente. Estoy atascado en configurar un delegado y no puedo resolver cuál es el problema.
Tengo una clase que extiende 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)
}
}
Luego en el ViewController se define como
class SettingsViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, SwitchCellDelegate {
y dentro del método
func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {
tenemos
case 2:
storeCredentialsCell = tableView.dequeueReusableCellWithIdentifier("StoreCredentialsCell") as? SwitchCell
if(storeCredentialsCell != nil){
...
NSLog("Setting delegate to %@ for %@", self.description, storeCredentialsCell.description)
storeCredentialsCell!.delegate = self
...
}
la salida del registro es la esperada, pero cuando alcanza la configuración real del delegado, la aplicación se bloquea con
EXC_BAD_ACCESS (código = 1, dirección = 0xfffffffffffffff8)
También debo tener en cuenta que si no configuro el valor del delegado cuando se delega? .SwitchChanged (self, state: swtSelector.on) también se produce un error EXC_BAD_ACCESS, pero según el doco para delegados, esto debería fallar correctamente si el delegado no está establecido en cualquier cosa.
===========================
He simplificado a un proyecto básico para replicar el 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)
}
}
Luego creé una escena de controlador de vista de tabla única con clase de TestTableViewController. La vista de tabla es dinámica con una sola celda de tipo TestCell. Esa celda contiene una etiqueta y un interruptor que están vinculados a los IBOutlets de la clase TestCell. La función switchChanged está vinculada al evento de cambio de valor en el switch.
Se produce el mismo error EXC_BAD_ACCESS.