Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/tfs/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 查找范围为3的数字_Swift - Fatal编程技术网

Swift 查找范围为3的数字

Swift 查找范围为3的数字,swift,Swift,我有一个来自JSON的数字,这个数字代表一个年龄。用户给了我两个年龄的范围,may code应该检查我从JSON获得的这个数字是否在这个范围内 这是我的代码,它给了我错误 表达式类型在没有更多上下文的情况下是不明确的 更新 如果let value:AnyObject=response.result.value作为AnyObject?{ var=String let json = JSON(value) for (key, subJson) in json { ages.ap

我有一个来自JSON的数字,这个数字代表一个年龄。用户给了我两个年龄的范围,may code应该检查我从JSON获得的这个数字是否在这个范围内

这是我的代码,它给了我错误

表达式类型在没有更多上下文的情况下是不明确的

更新

如果let value:AnyObject=response.result.value作为AnyObject?{ var=String

  let json = JSON(value)
  for (key, subJson) in json {

      ages.append(subJson["age"].string!)
 }
        guard let min = Int(self.DropDownFrom.selectedItem!) else { return }
        guard let max = Int(self.DropDownTo.selectedItem!) else { return }
     for fitage in ages {
        switch ages
           {
           case (min...max):
                print ("Age is in range")
                default:
         print ("Nope, not this time")
     }
    }

仍然给我一个错误。

您必须打开选项:

    if  Int(AgeFrom)!...Int(AgeTO)! ~= Int(age)! {
        print("yes")
    }

当然,这是一种不安全的解包方式,因为如果
age从
age到
age
的转换失败,它将崩溃。

您需要解包选项,因为
Int(:String)
方法可能没有有效答案

最好的方法是使用
guard

guard let min = Int(AgeFrom) else { return }
guard let max = Int(AgeTo)   else { return }
然后,您可以使用简单的
if
语句:

if (min <= age && age <= max)
{
    print ("Age is in range")
}
if-简单性和可读性 这是编程中的一个基本问题,用
if
检查一个可选值是否介于其他两个可选值之间:

if Int(AgeFrom)! <= Int(age)! && Int(AgeTO)! >= Int(age)! {
    print("It is in the range!")
}

第二种解决方案对于年龄值的多个情况要好得多,尤其是当年龄值超出范围时


希望有帮助!

您也可以使用可选绑定:

if let ageFrom = Int(ageFrom),
    let ageTo = Int(ageTo),
    ageFrom...ageTo ~= age
{
    print("yes")
} else {
    print("no")
}

如果Int(AgeFrom)
Int.init(String)
是一个可失败的初始值设定项,那么
Int(age)的类型
Int?
,不是
Int
。你需要打开它。谢谢你的回答!我也这么做了,但它给了我一个错误。因为我想我在循环一系列的叮咬,而不仅仅是一个数字。你能看看我更新的答案吗。我尝试了你的答案和其他答案,但它给了我相同的错误,因为我在循环一个数组,它不仅仅是一个数字
if Int(AgeFrom)! <= Int(age)! && Int(AgeTO)! >= Int(age)! {
    print("It is in the range!")
}
switch(Int(AgeFrom)! <= Int(age)!, Int(AgeTO)! >= Int(age)!){
    case (true,true): print("Yes, it fits the range")
    case (false,true): print("Too young!")
    case (true,false): print("Too old!")
}
if let ageFrom = Int(ageFrom),
    let ageTo = Int(ageTo),
    ageFrom...ageTo ~= age
{
    print("yes")
} else {
    print("no")
}