Reputation: 1356
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?
Upvotes: 1
Views: 3118
Reputation: 535944
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.
Upvotes: 5