contains() in Xcode 7 Beta 5

拥有回忆 提交于 2019-12-11 11:14:53

问题


so I'm having a problem with my contains method in beta 5. specifically it says it is unavailable when using this code:

class func createSlot(currentCards: [Slot]) -> Slot {
    var currentCardValues:[Int] = []

    for slot in currentCards {
        currentCardValues.append(slot.value)
    }
    var randomNumber:Int = Int(arc4random_uniform(UInt32(13)))
    while contains(currentCardValues, randomNumber + 1) {
        randomNumber = Int(arc4random_uniform(UInt32(13)))
    }

Any help would be appreciated, not sure if it is a problem with the beta or just my new working with Swift 2, as it works in Xcode 6


回答1:


The problem is that contains() is no longer a global method that accepts a sequence as an argument. Instead, the method must be called on the sequence

In your case, you should change contains(currentCardValues, randomNumber + 1) to currentCardValues.contains(randomNumber + 1)

Swift 1.x

let myNumbers: [Int] = [0, 1, 2, 3, 4]
let number: Int = 3
let contains: Bool = contains(myNumbers, number) //true

Swift 2.x

let myNumbers: [Int] = [0, 1, 2, 3, 4]
let number: Int = 3
let contains: Bool = myNumbers.contains(number) //true


来源:https://stackoverflow.com/questions/32159295/contains-in-xcode-7-beta-5

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!