0

I am getting the following Swift Compiler Warning: Expression implicitly coerced from 'UILabel?' to Any

In addition I found the following messages:

<unknown>:0: warning: expression implicitly coerced from 'UILabel?' to Any
<unknown>:0: note: provide a default value to avoid this warning
<unknown>:0: note: force-unwrap the value to avoid this warning
<unknown>:0: note: explicitly cast to Any with 'as Any' to silence this warning

But I am not exactly sure why. This is the code where I use the UILabel

class MyCell: UITableViewCell {
    var primaryLabel: UILabel!
    var secondaryLabel: UILabel!

    required init(coder aDecoder: NSCoder) {
        fatalError("init(coder:)")
    }

    init(_ row: myStruct) {
        super.init(style: .default, reuseIdentifier: "myId")
        primaryLabel = {
            let l = UILabel(frame: .zero)
            ...
            l.translatesAutoresizingMaskIntoConstraints = false
            return l
        }()
        self.addSubview(primaryLabel)

        if row.subtitle != nil {
            secondaryLabel = {
                let l = UILabel(frame: .zero)
                ...
                l.translatesAutoresizingMaskIntoConstraints = false
                return l
            }()
            self.addSubview(secondaryLabel)
        }

        ... 
    }
}

What am I doing wrong? Or how can I safely mute the compiler?

Thanks

Hamish
  • 78,605
  • 19
  • 187
  • 280
Joseph
  • 9,171
  • 8
  • 41
  • 67
  • Where is the `print` statement which causes the warning? in Swift 3 the warning occurs when trying to print an optional value. – vadian Feb 04 '17 at 17:56
  • 1
    On what line are you getting the warning? – Hamish Feb 04 '17 at 17:58
  • Can you please specify where the error occurs? – Mr. Xcoder Feb 04 '17 at 17:59
  • @Hamish It doesn't give a line number - I searched my source code for `UILabels` and this is pretty much one of the only spots I found them. Iget the warning a total of 5 times. – Joseph Feb 04 '17 at 17:59
  • @Xcoder123 when building. here an image: http://imgur.com/a/x9CFX – Joseph Feb 04 '17 at 18:01
  • Try cleaning your build folder and rebuilding @Joseph – the compiler should indicate the line (if it even still warns after rebuilding). Although now that I come to think of it, there is a problem in Xcode 8.2.1 where the compiler sometimes cannot locate the line of the warning (which is fixed in Xcode 8.3 beta). Might be worth downloading the beta to locate the warning. – Hamish Feb 04 '17 at 18:01
  • @vadian I am not printing anything with `UILabels` – Joseph Feb 04 '17 at 18:02
  • It's because you do not unwrap the optionals. Try using **guard**, **if-let** statements or (I strongly do not recommend this) force unwrap them. But it should specify the exact lines of the error... strange – Mr. Xcoder Feb 04 '17 at 18:02
  • @Hamish I tried Project > Clean and I did Watchdog > Clean Now. – Joseph Feb 04 '17 at 18:02
  • Are you sure that this class is causing the errors? – Mr. Xcoder Feb 04 '17 at 18:04
  • @Xcoder123 but they are not optional `var primaryLabel: UILabel!` – Joseph Feb 04 '17 at 18:04
  • 1
    I suspect the error occurs somewhere else. Is anything displayed in the Issue Navigator (⌘4)? – vadian Feb 04 '17 at 18:05
  • It is totally not caused by this class, search somewhere else in your project – Mr. Xcoder Feb 04 '17 at 18:06
  • @vadian nope http://imgur.com/a/xOi9N – Joseph Feb 04 '17 at 18:07
  • Check all your print statements everywhere. Once again this warning is displayed typically when printing optionals. And usually when you click on the warning in Issue Navigator it shows you the line in code. – vadian Feb 04 '17 at 18:10
  • If you click on the warnings, does it "jump" to any section of your code, if so where? – Mr. Xcoder Feb 04 '17 at 18:10
  • @Joseph Do you have any `[UILabel?]`'s in your project? Could be related to http://stackoverflow.com/q/41874682/2976878. Like I said in my previous (edited) comment, the simplest way to locate the warning would be to download Xcode 8.3 beta, which fixed the issue of not being able to find the line for this given warning. – Hamish Feb 04 '17 at 18:11
  • @Xcoder123 nope - that is what I can't explain either. I don't have any other `UILabels` in my code. – Joseph Feb 04 '17 at 18:13
  • @vadian only have 1 print statement in my code and it prints the object created a few lines above it. commenting it out had no effect. – Joseph Feb 04 '17 at 18:13
  • try closing and reopening Xcode and do thatt again – Mr. Xcoder Feb 04 '17 at 18:13
  • @Hamish nope like I wrote these are the only `UILabel`s in use. To make sure I commented the other two instances of `UILabel` out and still got the error. Restarted Xcode as well. – Joseph Feb 04 '17 at 18:15
  • After restarting I do now get a slightly changed error message. It has to be this file: http://imgur.com/a/fs4EP and those are the only `UILabels` in the file – Joseph Feb 04 '17 at 18:17

0 Answers0