'[(UIView)]' is not identical to 'UInt

2019-02-21 22:04发布

问题:

I was using += to an a UIView to an array and that no longer seems to work. The line

dropsFound += hitView

Gives an error '[(UIView)]' is not identical to 'UInt8'

Here is part of the method. Note that as of Xcode 6 beta 5, hitTest now returns an optional, so it was necessary to say

hitView?.superview

instead of

hitView.superview

in the 'if' statement.

func removeCompletedRows() -> Bool {
    println(__FUNCTION__)
    var dropsToRemove = [UIView]()

    for var y = gameView.bounds.size.height - DROP_SIZE.height / 2; y > 0; y -= DROP_SIZE.height {
        var rowIsComplete = true
        var dropsFound = [UIView]()
        for var x = DROP_SIZE.width / 2; x <= gameView.bounds.size.width - DROP_SIZE.width / 2; x += DROP_SIZE.width {
            let hitView = gameView.hitTest(CGPointMake(x, y), withEvent: nil)
            if hitView?.superview === gameView {
                dropsFound += hitView
            } else {
                rowIsComplete = false
                break
            }
        }

... remainder of method omitted

回答1:

That changed in the last release. From the beta 5 release notes:

The += operator on arrays only concatenates arrays, it does not append an element. This resolves ambiguity working with Any, AnyObject and related types.

So if the left side of += is an array, the right now must be as well.

so:

dropsFound.append(hitView)

Or if you really wanted to use += you could probably do:

dropsFound += [hitView]

But that would be a little silly. Use append like the error message suggests.



回答2:

The solution appears to be that you need to use the append method for the array rather than +=. I don't know the reason for this, so another answer might be more appropriate.

Instead of

dropsFound += hitView

use

dropsFound.append(hitView!)

Again, note that the UIView returned from hitTest is an optional as of Xcode 6 beta 5.

I verified that this is a general problem with arrays with the following playground sample. A bug report has been posted to Apple.

var s: [String] = []
// s += "hello"  // error: '[String]' is not identical to 'UInt8'
s.append("hello")
s

There is an additional complexity if you are trying to append a tuple, and perhaps other types.

// line below no longer works in Xcode 6 beta 5
// and you will also get an error trying to append the tuple directly
// which is probably a bug
// possibleFlipsArray += (x, y)
// possibleFlipsArray.append((x, y))
let tempTuple = (x, y)
possibleFlipsArray.append(tempTuple)

This probably deserves its own question, though I think it is just another bug, so again I've posted it to Apple.



回答3:

adding object in array dropsFound += hitView,in this way, is removed in last release. You can add element in array by using this syntax dropsFound += [hitView] or dropsFound.append(hitView)



标签: uiview swift