'[(UIView)]' is not identical to 'UInt8' when using += in Xcode 6 beta 5. Use append method instead?

前端 未结 3 482
独厮守ぢ
独厮守ぢ 2021-01-18 05:35

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

相关标签:
3条回答
  • 2021-01-18 06:08

    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.

    0 讨论(0)
  • 2021-01-18 06:12

    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.

    0 讨论(0)
  • 2021-01-18 06:12

    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)

    0 讨论(0)
提交回复
热议问题