简体   繁体   中英

In swift I want to check if the number which I passed as a parameter to the function is in the array ( which aslo I passed as a parameter)

func check (num: Int, arr: Array<Any>) -> Bool {
    if arr.contains(where: num) {
        return true
    }
}
check(num: 7, arr: [1, 4, 7, 25, 99])

// eror: Cannot convert value of type 'Int' to expected argument type '(Any) throws -> Bool'

You are doing lot of incorrect coding in the above question. So it is better to take a look at here -> Functions in Swit .

  1. when u are passing an array as a parameter u should use [DataTpye] signature.
  2. The method use to check if an element is in an array is this not this .
  3. Your whole function should return a boolean value not only the if part.

Corrected version

func check (num: Int, arr: [Int]) -> Bool {
    return arr.contains(num)
}
print(check(num: 7, arr: [1, 4, 7, 25, 99])) // true

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM