2016-10-04 145 views
-1

我在tableViewCell正確顯示每label,但只有我attributeString輸出爲「optional(1)迅速 - 細胞label.text是可選

 let mString = String(self.productList?["market_price"].int) 
    let attributeString: NSMutableAttributedString = NSMutableAttributedString(string: mString) 
    attributeString.addAttribute(NSStrikethroughStyleAttributeName, value: 2, rnge: NSMakeRange(0, attributeString.length)) 
    attributeString.addAttribute(NSStrikethroughColorAttributeName, value: UIColor.lightGrayColor(), range: NSMakeRange(0, attributeString.length)) 
    self.marketPriceLabel.attributedText = attributeString 
+1

的原因可能是神祕的'.int'功能,這似乎返回一個可選的,並且'String'初始化對待可選字面上。無論如何,'productList?[「market_price」]'的結果是可選的。 – vadian

回答

0

我找到了解決辦法。

let mInt: Int = (self.productList?["market_price"].int)! 
let mString: String = String(mInt) 
let attributeString: NSMutableAttributedString = NSMutableAttributedString(string: mString) 
attributeString.addAttribute(NSStrikethroughStyleAttributeName, value: 2, rnge: NSMakeRange(0, attributeString.length)) 
attributeString.addAttribute(NSStrikethroughColorAttributeName, value: UIColor.lightGrayColor(), range: NSMakeRange(0, attributeString.length)) 
self.marketPriceLabel.attributedText = attributeString