2017-10-14 58 views
0

我對結果複製按鈕代碼(從displayResultLabel如何複製兩個標籤的結果?

我怎樣才能知道它是從該兩個標籤一次(從resultLabel,displayResultLabel

resultLabel複製 - 歷史

displayResultLabel - 結果


實施例:

resultLabel - 5 + 22

displayResultLabel - 27

5 + 22 = 27

Photo


我想這一點,但我得到這個:可選( 「5 + 22」)=可選( 「27」)

UIPasteboard.general.string = "\(String(describing: self.resultLabelText.text)) = \(String(describing: self.displayResultLabel.text))" 

按鈕複製

... 
let deleteActions = UIAlertAction(title: NSLocalizedString("Copy history",comment: ""), style: .default, handler: { 
     (alert: UIAlertAction!) -> Void in 
     UIPasteboard.general.string = self.resultLabelText.text 

     self.present(alert, animated: true, completion:nil) 
    }) 
... 

回答

0

這是因爲標籤文本是可選的,這就是爲什麼你會得到:

Optional ("5 + 22") = Optional ("27") 

像這樣做,而不是:

if let resultText = resultLabelText.text, let displayText = displayResultLabel.text { 
    UIPasteboard.general.string = "\(resultText) = \(displayText)" 

} 
+0

非常感謝您 – DTF

0

你只需要因爲self.resultLabelText.textself.displayResultLabel.text是自選意味着返回值可以是零或有一個值來解開你的價值觀。

如果你確定你有一個值,它可以通過像這樣的感嘆號來打開它們:self.resultLabelText.text!和self.displayResultLabel.text!或使用作爲Rashwan提議的可選綁定。這是最開心的方式!

蘋果文檔的基礎:Basics

+0

如果可能,嘗試不給力展開值。 –

+0

這就是爲什麼我提到可選綁定;) – Arrabidas92