簡體   English   中英

無法強制展開非可選類型“Bool”的值

[英]Cannot force unwrap value of non-optional type 'Bool'

我是 Swift 的新手(使用 Swift 4.1)。 我正在學習一些教程來嘗試學習語法。 在代碼行中:

if !message.isSender!.boolValue

我收到一條錯誤消息:

無法強制展開非可選類型“Bool”的值

我已經四處瀏覽,但無法完全找到或找出錯誤的解決方案。

if !message.isSender!.boolValue {
    cell.messageTextView.frame = CGRect(x: 48 + 8, y: 0, width: estimatedFrame.width + 16, height: estimatedFrame.height + 20)

    cell.textBubbleView.frame = CGRect(x: 48, y: 0, width: estimatedFrame.width + 16 + 8, height: estimatedFrame.height + 20)
} else {
    cell.messageTextView.frame = CGRect(x: 48 + 8, y: 0, width: estimatedFrame.width + 16, height: estimatedFrame.height + 20)

    cell.textBubbleView.frame = CGRect(x: view.frame.width - estimatedFrame, y: 0, width: estimatedFrame.width + 16 + 8, height: estimatedFrame.height + 20)
}

由於它已經是一個布爾值,只需使用它:

if !message.isSender

沒有理由解包,它不是編譯器告訴您的可選,並且沒有理由訪問boolValue以獲取簡單的if

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM