1

I've included a set of swift classes and their swift dependencies into my Objective-C project. I've already done this for other swift libraries, so things like the Obj-C Generated Interface Header are already present.

This is the class I wish to use:

    @objc public class StatusBarNotificationBanner: BaseNotificationBanner 
    {
    override init(style: BannerStyle) {
        super.init(style: style)
        bannerHeight = 20.0

        titleLabel = MarqueeLabel()
        titleLabel?.animationDelay = 2
        titleLabel?.type = .leftRight
        titleLabel!.font = UIFont.systemFont(ofSize: 12.5, weight: UIFontWeightBold)
        titleLabel!.textAlignment = .center
        titleLabel!.textColor = .white
        addSubview(titleLabel!)

        titleLabel!.snp.makeConstraints { (make) in
            make.top.equalToSuperview()
            make.left.equalToSuperview().offset(5)
            make.right.equalToSuperview().offset(-5)
            make.bottom.equalToSuperview()
        }

        updateMarqueeLabelsDurations()
    }

    public convenience init(title: String, style: BannerStyle = .info) {
        self.init(style: style)
        titleLabel!.text = title
    }

    public convenience init(attributedTitle: NSAttributedString, style: BannerStyle = .info) {
        self.init(style: style)
        titleLabel!.attributedText = attributedTitle
    }

    required public init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }

}

This is how one would use the class in swift:

let banner = StatusBarNotificationBanner(title: title, style: .success)
banner.show()

How in Obj-C would I instantiate StatusBarNotificationBanner and call its show() method?

Also, how do I pass the enum parameter style?

This is the enum:

public enum BannerStyle {
    case danger
    case info
    case none
    case success
    case warning
}

I guess that the enum needs to take the form:

@objc public enum BannerStyle: Int {
    case danger
    case info
    case none
    case success
    case warning
}

But I still don't know how to pass it as a param in Obj-C and I'm not understanding why the Int has to be specified? Isn't the enum implicitly Int?

1 Answer 1

5

Isn't the enum implicitly Int?

Not really. Objective-C cannot see a Swift enum at all. In Swift, an enum is an object type. Objective-C has no knowledge whatever of any such object type; its only objects are classes. (In Objective-C, enums are just numbers with names.) Therefore, neither a Swift enum type, nor a method that takes or produces a Swift enum, nor a Swift enum property, is exposed to Objective-C

However, in the special case where you say @objc enum BannerStyle: Int, it is translated into an Objective-C enum for you. So, in Objective-C, names such as BannerStyleDanger and BannerStyleInfo will spring to life. But they will just be integers.

Sign up to request clarification or add additional context in comments.

7 Comments

Thanks for answering the part of the question about the enum, but my bigger issue and question is how to instantiate this class and call the show method (with the enum value as one of the params) from Objective-C.
You can easily find this all out by looking at the automatically generated Objective-C bridging header. The easiest way is to command-click the name of the bridging header where your Objective-C file imports it.
Same answer. Look in the bridging header and see what the Objective-C initializer is for this class.
Thanks that helped for the init, but I'm still not seeing the show method which is in the base class. I prefixed the base class with @objc but I'm not seeing anything in the bridging header for the method. It is public.
Well, you have not shown the base class so I don't know anything about it. I know nothing about the show method either.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.