2017-04-27 20 views
0

使用雨燕類和枚舉我已經包含了一組迅速類及其迅速的依賴關係到我的Objective-C的項目。我已經爲其他swift庫完成了這個工作,所以Obj-C生成的接口頭文件已經存在。從Objective-C的

這是我想使用的類:

@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() 

如何在對象 - 我想實例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 PARAM -C和我不明白爲什麼必須指定Int?是不是隱式的Int?

回答

3

是不是枚舉隱含詮釋?

不是。 Objective-C根本看不到Swift枚舉。在Swift中,枚舉是一種對象類型。 Objective-C不知道任何這樣的對象類型;它唯一的對象是類。 (在Objective-C中,枚舉只是具有名稱的數字。)因此,無論是Swift枚舉類型還是生成Swift枚舉或Swift枚舉屬性的方法都不會暴露於Objective-C

,在你說@objc enum BannerStyle: Int的特殊情況下,它會被翻譯成你的Objective-C枚舉類型。因此,在Objective-C,名字如BannerStyleDangerBannerStyleInfo將春天的生命。但他們只是整數。

+0

感謝您回答有關枚舉問題的部分,但我的更大的問題是如何實例化此類並從Objective-C調用show方法(將枚舉值作爲params之一)。 – bhartsb

+0

通過查看自動生成的Objective-C橋接頭,您可以輕鬆找到這一切。最簡單的方法是命令單擊您的Objective-C文件導入橋接頭的名稱。 – matt

+0

同樣的答案。查看橋接頭並查看該類的Objective-C初始化程序。 – matt