Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Impossible to subclass YoshiSingleSelectionMenu with weak self in handler #77

Open
krin-san opened this issue Mar 27, 2018 · 0 comments

Comments

@krin-san
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant