Search code examples
iosswiftxcodewarningsselector

Xcode 13.3 warning: "'self' refers to the method '{object}.self', which may be unexpected


I just updated to Xcode 13.3 and I'm seeing several instances of a new warning that I've not seen with previous versions of Xcode. As an example, I have a simple table view cell named LabelAndSwitchTableViewCell that looks like this:

import UIKit

class LabelAndSwitchTableViewCell: UITableViewCell {

    private let label: UILabel = {
        let label = UILabel()
        label.translatesAutoresizingMaskIntoConstraints = false
        return label
    }()

    private let _switch: UISwitch = {
        let _switch = UISwitch()
        _switch.translatesAutoresizingMaskIntoConstraints = false
        _switch.addTarget(self, action: #selector(didToggleSwitch), for: .valueChanged)
        return _switch
    }()

    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        contentView.addSubview(label)
        contentView.addSubview(_switch)

        // layout constraints removed for brevity
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
        
    @objc private func didToggleSwitch() {
        print("Switch was toggled...")
    }
}

As you can see, I'm adding a target to the switch that I want to be called when the value of the switches changes:

_switch.addTarget(self, action: #selector(didToggleSwitch), for: .valueChanged)

After updating to Xcode 13.3, I'm now seeing a new warning on this line:

'self' refers to the method 'LabelAndSwitchTableViewCell.self', which may be unexpected

Xcode's suggestion to silence this warning is to replace:

_switch.addTarget(self, action: #selector(didToggleSwitch), for: .valueChanged)

...with...

_switch.addTarget(LabelAndSwitchTableViewCell.self, action: #selector(didToggleSwitch), for: .valueChanged)

Making this change does silence the warning but it also causes the app to crash (unrecognized selector) when I toggle the switch. Here's the dump from that crash:

[app_mockup.LabelAndSwitchTableViewCell didToggleSwitch]: unrecognized selector sent to class 0x1043d86e8
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '+[app_mockup.LabelAndSwitchTableViewCell didToggleSwitch]: unrecognized selector sent to class 0x1043d86e8'

Making the didToggleSwitch() method static will prevent the crash but I'm not sure why I'd want to do that. I can obviously revert the change (from LabelAndSwitchTableViewCell.self back to just self) but I'm wondering if there's something else that I should be doing to address this?


Solution

  • You can fix by changing the lets to lazy var's

    private lazy var _switch2: UISwitch = {
        let _switch = UISwitch()
        _switch.translatesAutoresizingMaskIntoConstraints = false
        _switch.addTarget(self, action: #selector(didToggleSwitch), for: .valueChanged)
        return _switch
    }()
    

    The Xcode fix-it suggestion is just wrong.