简体   繁体   English

斯威夫特:返回数字 < 10

[英]Swift: return number < 10

That's the code:那是代码:

import Foundation

func hasAnyMatches(list: [Int], condition: Int -> Bool) -> Bool {
    for item in list {
        if condition(item) {
            return true
        }
    }
    return false
}

func lessThanTen(number: Int) -> Bool {
    return number < 10
}

var numbers = [20, 19, 7, 12]
hasAnyMatches(numbers, condition: lessThanTen)

There is a return true within the if condition and a return false at the end of func hasAnyMatches() .有一种return true,如果条件之内,并且return false在年底func hasAnyMatches() Why is the return false needed?为什么需要return false

Without explicitly "telling it" to return something, no function returns false "automatically".如果没有明确“告诉它”返回某些东西,则没有函数“自动”返回false I am not writing in swift but I am sure if you take away the return false it'll throw a warning.我没有快速写作,但我相信如果你取消return false它会发出警告。

The lessThanTen simply returns a bool if the number is smaller than 10.如果数字小于 10, lessThanTen只会返回一个布尔值。

number < 10 is a statement checking if something is true or false . number < 10是检查某事是true还是false的语句。 Hence the return type bool of lessThanTen因此lessThanTen的返回类型bool

You can do like, for such condition.对于这种情况,您可以这样做。

let LESS_THAN_TEN = 1
let LESS_THAN_HUNDRED = 2

func hasAnyMatch (list: [Int], condition: Int)-> Bool{
 var x = false;
 switch  condition {
 case LESS_THAN_TEN :
    for a in list {
        if a < 10 {
            x = true
                break
            }
        }
 break

 case LESS_THAN_HUNDRED :
    for a in list {
        if a < 100 {
            x = true
            break
        }
    }

 break

 default:
    x = false
}
   return x
}

var isLessThanTen = hasAnyMatch([14, 45, 12, 65], condition: LESS_THAN_TEN)
print(isLessThanTen)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Swift-在小数点后十位 - Swift - Get 10's place of a decimal number 如何将电话号码返回给swift应用程序? - How to return a phone number to swift app? 如何从 Swift 中的 JSON 中的 collectionview numberOfItemsInSection 返回 Max 10 项 - How to return Max 10 Items in collectionview numberOfItemsInSection from JSON in Swift 如果 swift 中的数字超过“10”,如何删除小数点 - How to remove decimal point if the number more than “10” in swift iOS 10 Swift电话未在某些号码上启动 - ios 10 Swift phone call is not initiated on some number Swift JSON 解析问题,无法返回正确数量的单元格 - Swift JSON Parsing problem, cannot return proper number of cells 如何在此Swift函数中减少return false的数量 - How can I reduce the number of return false in this Swift func Swift 日期组件错误返回日期天数 - Swift date components incorrect return of date day number 如果在快速表视图中有更多(大约10到20个)部分,如何返回分区值? - How to return with section value, if there more(approx.,10 to 20) sections in swift table view? swift void SendDelegateMessage .. webView:decidePolicyForNavigationAction等待10秒后无法返回…kCFRunLoopDefaultMode - swift void SendDelegateMessage.. webView:decidePolicyForNavigationActionfailed to return after waiting 10 seconds… kCFRunLoopDefaultMode
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM