【发布时间】:2017-04-27 03:10:41
【问题描述】:
我在我的 Objective-C 项目中包含了一组 swift 类及其 swift 依赖项。我已经为其他 swift 库做了这个,所以像 Obj-C Generated Interface Header 这样的东西已经存在。
这是我希望使用的类:
@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)
}
}
这是在 swift 中使用类的方式:
let banner = StatusBarNotificationBanner(title: title, style: .success)
banner.show()
我如何在 Obj-C 中实例化 StatusBarNotificationBanner 并调用它的 show() 方法?
另外,如何传递枚举参数样式?
这是枚举:
public enum BannerStyle {
case danger
case info
case none
case success
case warning
}
我猜枚举需要采取以下形式:
@objc public enum BannerStyle: Int {
case danger
case info
case none
case success
case warning
}
但我仍然不知道如何在 Obj-C 中将它作为参数传递,而且我不明白为什么必须指定 Int?枚举不是隐含的 Int 吗?
【问题讨论】:
标签: objective-c swift class enums