Failed to distinguish type value

I am creating a Tic Tac Toe application. When the game is reset, all Xs and Os images UIButtonshould return to nil. Each place on the board is marked from 0 to 8.

  var buttonsToClear : UIButton

    for var i = 0; i < 9; i++ {

        buttonsToClear = view.viewWithTag(i) as! UIButton

        buttonsToClear.setImage(nil, forState: .Normal)

    }

Everything worked fine until I added a navigation bar. Now i get an error

"Failed to distinguish value of type _UINavigationBarBackground '(0x107e2f2a0)> to' UIButton '(0x107e357e0). (Lldb)

I am absolutely sure that my new navigation bar has a "10" tag.

Navigation bar

What else could cause this message?

+4
source share
5 answers

You are logically right and your code will work just fine.

You get an error message

" _UINavigationBarBackground ' (0x107e2f2a0) > 'UIButton' (0x107e357e0). (Lldb)

, for loop , i.e. = 0,

buttonsToClear = view.viewWithTag(i) as! UIButton

buttonsToClear = view.viewWithTag(0) as! UIButton

, tag 0.

UINavigationBarBackground 0.

:

1. , , viewWithTag:, .

2. 0.

3. UILabel UIView XIB .

, UINavigationBarBackground UIButton. UINavigationBarBackground UIButton.

:

, 0. , 1000. 1000, 1001, 1002 .

for, .

var buttonsToClear : UIButton

    for var i = 1000; i < 1009; i++ {

        buttonsToClear = view.viewWithTag(i) as! UIButton

        buttonsToClear.setImage(nil, forState: .Normal)

    }
+5

0 - 0 , ( ).

, 1.

+4

, , ? , , .

, Tag 0.

+1

,


, UINavigationBar, _UINavigationBarBackground. , 0. UIButton.

, UIButton, ( 0, ) . , 100% , .

var buttonsToClear : UIButton

for view in view.subviews { // Loop through subviews
    if let button = view as? UIButton { // Check if UIButton
        if button.tag > 0 || button.tag < 10 { // Check tags
            buttonsToClear = button
            buttonsToClear.setImage(nil, forState: .Normal)
        }
    }
}
0

- UIBarButtonItem a UIButton, , , , UINavigationBar, UIBarButtonItem.

, , . IBOutlet , . , , IBOutletCollection, , UIBarButtonItem, UIButton.

leftBarButtonItem UIBarButtonItem , a UIButton, , UIBarButtonItem!

-1
source

Source: https://habr.com/ru/post/1625387/


All Articles