我最近開始學習swift,並在swift 3中做類別。我在Viewcontroller A中添加了一個擴展,並添加了一個函數來刪除對象類CustomText.Now中的label.text,everythiing是做我唯一缺少的是對這個新擴展方法的調用。 下面是代碼:類別調用swift 3
ViewControllerA
extension String {
func setLabelText(){
let cell = ProductListingCell()
let text = CustomText()
cell.discountLabel.attributedText = text.getTextToStrikeThrough(label:cell.discountLabel)
}
CustomText
func getTextToStrikeThrough(label:UILabel) -> NSAttributedString {
let attributeString: NSMutableAttributedString = NSMutableAttributedString(string:label.text!)
attributeString.addAttribute(NSStrikethroughStyleAttributeName, value: 2, range: NSMakeRange(0, attributeString.length))
label.attributedText = attributeString
let str = label.attributedText
return str!
}
請幫助我明白這是如何擴展字符串應在功能帶來,讓調試器的推移它並執行所需task.Thanks提前!
這不會真正爲你工作;你只是創建一個單元格的新實例並在其上設置一些文本。該單元不顯示在任何地方,只要函數退出,它就會被拋棄。如果你想做這個擴展,那麼它應該是'UILabel'上的擴展,然後你會說'someLabel.setStrikethroughText(myString)' – Paulw11