Navigation Menu

Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SR-7906] assigning cast value to implicitly unwrapped Optional causes (bogus?) warning #50441

Open
mattneub opened this issue Jun 7, 2018 · 1 comment
Labels
compiler The Swift compiler in itself improvement

Comments

@mattneub
Copy link

mattneub commented Jun 7, 2018

Previous ID SR-7906
Radar None
Original Reporter @mattneub
Type Improvement
Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Improvement
Assignee None
Priority Medium

md5: e4c4e6ffbd6471a88e824e734ac2dba8

is duplicated by:

  • SR-7939 Spurious Warning when down casting to [String]

relates to:

  • SR-4209 Unnecessary message for non-optional to non-optional cast

Issue Description:

I have this code in my view controller:

    var accessoryView : UIView!
    override func viewDidLoad() {
        super.viewDidLoad()
        let arr = UINib(nibName:"AccessoryView", bundle:nil).instantiate(withOwner:nil)
        self.accessoryView = arr[0] as! UIView
    }

This elicits a warning: "Treating a forced downcast to 'UIView' as optional will never produce 'nil'"

Now, I have to cast `arr[0]` down because it arrives as an array of Any. And I have to make `accessoryView` an Optional because I won't have its value until `viewDidLoad`. And I want to make it an implicitly unwrapped Optional because I intend to use it a lot and I don't want a bunch of extra punctuation in my code.

So it seems to me that this warning is too zealous; I'm being "punished" for doing something quite natural and called for by the situation.

I have no objection to changing `as!` to `as?` and silencing the warning. But it seems silly that I should have to, when I know for a fact that this Any is a UIView. Why should I be forced to express doubt about something of which I'm certain.

I also have no objection to wrapping the cast in parentheses to silence the warning.

So I'm not saying that there are no ways out of this warning. It's not a problem; I can silence the warning and move on. I'm just wondering whether the warning should be reconsidered; perhaps, in our zeal to recast IUOs as normal Optionals with special behavior, we may have tightened up the rules a little too far.

@belkadan
Copy link
Contributor

belkadan commented Jun 8, 2018

I've got a dup for this, but the simplest answer may be just to silence this warning when not providing a variable's initial value.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler The Swift compiler in itself improvement
Projects
None yet
Development

No branches or pull requests

2 participants