Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.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错误:二进制运算符==不能应用于类型'_';和';Int';_Swift - Fatal编程技术网

Swift错误:二进制运算符==不能应用于类型'_';和';Int';

Swift错误:二进制运算符==不能应用于类型'_';和';Int';,swift,Swift,我是Swift的新手,我正在尝试构建一个简单的程序,根据用户的年龄告诉他们出生在中国日历的哪一年 var string1 = "You are year of the" let age:Int? = Int(ageField.text!) if age <= 12 { let remainder = age! } else { let remainder = age! % 12 } if remainder

我是Swift的新手,我正在尝试构建一个简单的程序,根据用户的年龄告诉他们出生在中国日历的哪一年

    var string1 = "You are year of the"
    let age:Int? = Int(ageField.text!)

    if age <= 12 {
        let remainder = age!
    } else {
        let remainder = age! % 12
    }

    if remainder == 0 {
        string1 += " sheep."
    }; if remainder == 1 {
        string1 += " horse."
    }; if remainder == 2 {
        string1 += " snake."
    }; if remainder == 3 { // And so on and so forth...
var string1=“你们是一年”
let age:Int?=Int(ageField.text!)

如果年龄变量/常量
余数
应在
if
构造之外声明,并且您还可以删除代码中的“;”字符。Swift不需要在指令末尾使用“;”,如objective-c

变量/常数
余数
应在
if
构造之外声明,您也可以删除代码中的“;”字符。Swift不需要像objective-c那样在指令末尾加“;”作为Alessandro答案的摘要和优化代码的注释

var string1 = "You are year of the"
if let age = Int(ageField.text!) {

    let remainder = age % 12

    if remainder == 0 {
        string1 += " sheep."
    } else if remainder == 1 {
        string1 += " horse."
    } else if remainder == 2 {
        string1 += " snake."
    } // And so on and so forth...

} else {
    print("please enter a number")
}
或者使用
开关
语句稍微“快一点”

var string1 = "You are year of the "
if let age = Int(ageField.text!) {

    switch age % 12 {

    case 0: string1 += "sheep."
    case 1: string1 += "horse."
    case 2: string1 += "snake."
        // And so on and so forth...
    }

} else {
    print("please enter a number")
}

附:事实上,羊是山羊;-)

作为Alessandro答案和您优化代码的注释的总结

var string1 = "You are year of the"
if let age = Int(ageField.text!) {

    let remainder = age % 12

    if remainder == 0 {
        string1 += " sheep."
    } else if remainder == 1 {
        string1 += " horse."
    } else if remainder == 2 {
        string1 += " snake."
    } // And so on and so forth...

} else {
    print("please enter a number")
}
或者使用
开关
语句稍微“快一点”

var string1 = "You are year of the "
if let age = Int(ageField.text!) {

    switch age % 12 {

    case 0: string1 += "sheep."
    case 1: string1 += "horse."
    case 2: string1 += "snake."
        // And so on and so forth...
    }

} else {
    print("please enter a number")
}

附:事实上,羊是山羊;-)

假设
年龄您需要测试
年龄
是否为
nil
let age:Int=Int(ageField.text!)
where
ageField.text
==“Grimxn”将导致您以后的作业崩溃(一旦编译)-请参阅下面@AlessandroChiarotto的答案。假设
age您需要测试
age
是否为
nil
let age:Int=Int(ageField.text!)
where
ageField.text
==“Grimxn”将导致您以后的作业崩溃(编译完成后)-请参阅下面@AlessandroChiarotto的答案。