Skip to content

Impossible to subclass YoshiSingleSelectionMenu with weak self in handler #77

Description

@krin-san

Current implementation of YoshiSingleSelectionMenu doesn't allow to use weak self in didSelect handler because upon calling super.init in subclass initializer self will be passed before finishing initialization and app will crash with EXC_BAD_ACCESS exception.

Demo

import UIKit
import Yoshi

/// Oversimplified class I have on a project to stub API requests with OHHTTPStubs library
class APIDebugMenu: YoshiSingleSelectionMenu {

    var activeStub: String? {
        didSet {
            print("Set to \(String(describing: activeStub))")
        }
    }

    init() {
        let options = [
            YoshiSingleSelection(title: "Off", subtitle: nil),
            YoshiSingleSelection(title: "HTTP 404", subtitle: nil),
            YoshiSingleSelection(title: "HTTP 503", subtitle: nil),
        ]
        super.init(title: "API", options: options, selectedIndex: 0) { [weak self] (item) in
            self?.activeStub = item.title
        }
    }

}

class YoshiExcBadAccess: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        let item = APIDebugMenu()
        Yoshi.setupDebugMenu([item])
    }

}

It's possible to refactor our implementation so the activeStub will be managed by an object retained in a closure to fix this issue. But what the purpose of having open class YoshiSingleSelectionMenu?

Possible solution

Make didSelect public so it can be written after initialization.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions