Uploaded image for project: 'Swift'
  1. Swift
  2. SR-10952

xCode shows warnings about casting optional(?) value to Any while it unwrapped(!)

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Open
    • Priority: Medium
    • Resolution: Unresolved
    • Labels:
      None
    • Environment:

      macOS ‏10.14.4 ‏(18E226‏)

      xCode 11.0 beta (11M336w)

      Description

      When trying to pass value with type `Type!`, Xcode shows this warning:

      Coercion of implicitly unwrappable value of type 'Type?' to 'Any' does not unwrap optional
      

      As you can see, Xcode ignoring that the type is already unwrapped and treats it as an optional value.

       

      For example:

      @IBDesignable
      class MyLabel: UILabel {
          @IBInspectable var paddingTopBottom: CGFloat = 0
          @IBInspectable var paddingRightLeft: CGFloat = 0
      
          override func drawText(in rect: CGRect) {
              let padding = UIEdgeInsets(top: paddingTopBottom, left: paddingRightLeft, bottom: paddingTopBottom, right: paddingRightLeft)
      
      
              guard let labelText = text else { return super.drawText(in: rect.inset(by: padding)) }
      
      
              var newRect = rect
              if labelText.matches("[א-ת]") {
                  let attributedText = NSAttributedString(string: labelText, attributes: [.font: font])
      
      
                  newRect.size.height = attributedText.boundingRect(with: rect.size, options: .usesLineFragmentOrigin, context: nil).size.height
      
      
                  if numberOfLines != 0 {
                      newRect.size.height = min(newRect.size.height, CGFloat(numberOfLines) * font.lineHeight)
                  }
              }
              super.drawText(in: newRect.inset(by: padding))
          }
      }
      

      You may ignore the logic itself and focus on this line:

      NSAttributedString(string: labelText, attributes: [.font: font])

       

      I'm getting the warning on the `font` variable, although you know it's defined as unwrapped value in the `UILabel` class:

      open class UILabel : UIView, NSCoding, UIContentSizeCategoryAdjusting {
          ...
          open var font: UIFont!
          ...
      }
      

       

        Attachments

          Activity

            People

            Assignee:
            Unassigned Unassigned
            Reporter:
            idomo Ido
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Dates

              Created:
              Updated: