基于字符串切换语句?

时间:2017-07-30 16:47:59

标签: ios swift switch-statement

我有一串通过文本字段输入的指令,我想获取最新值并根据其值执行操作。

问题是,为什么这段代码不起作用? 'instruction'打印我想要正确使用的字符串值,但是我总是得到“不是一个公认的字符”默认情况......

    func processInputCommand() {
    print("processing called")
    let string = self.robotInstructions.text!
    let commands = Array(string.characters)
    let instruction = String(describing: commands.last)
    print(instruction)

        switch instruction {
        case "N" :
            self.usersRobot.yPosition = self.usersRobot.yPosition + 1
        case "S" :
            self.usersRobot.yPosition = self.usersRobot.yPosition - 1
        case "E" :
            self.usersRobot.xPosition = self.usersRobot.xPosition + 1
        case "W" :
            self.usersRobot.xPosition = self.usersRobot.xPosition - 1
        case "P" :
            pickUpGummyBears()
        case "D" :
            if self.usersRobot.numberOfBagsHeld > 0 {
                dropGummyBears()
            } else {
                print("THE ROBOT ISNT HOLDING ANY BAGS CURRENTLY")
            }
        default:
            print("WASNT RECOGNISED CHARACTER")
        }
}

3 个答案:

答案 0 :(得分:2)

问题是以下行中的command.last会返回一个可选项。您需要打开值:

let instruction = String(describing: commands.last)

像这样修改你的代码:

let commands = Array(string.characters)
//unwrap the value here
guard let last = commands.last else {return}
let instruction = String(describing: last)
print(instruction)

答案 1 :(得分:1)

string instructionfunc processInputCommand() { print("processing called") guard let string = self.robotInstructions.text, let instruction = string.characters.last else { print("INVALID INPUT") return } switch instruction { case "N" : self.usersRobot.yPosition = self.usersRobot.yPosition + 1 case "S" : self.usersRobot.yPosition = self.usersRobot.yPosition - 1 case "E" : self.usersRobot.xPosition = self.usersRobot.xPosition + 1 case "W" : self.usersRobot.xPosition = self.usersRobot.xPosition - 1 case "P" : pickUpGummyBears() case "D" : if self.usersRobot.numberOfBagsHeld > 0 { dropGummyBears() } else { print("THE ROBOT ISNT HOLDING ANY BAGS CURRENTLY") } default: print("WASNT RECOGNISED CHARACTER") } } 使用安全的方式:

typeof C

答案 2 :(得分:0)

如Hussain Shabbir所述,commands.last的类型为Optional<Character>(又名Character?)。直接在switch语句中使用它的示例。

func processInputCommand() {
    print("processing called")
    let string = self.robotInstructions.text!
    let commands = string.characters
    let instruction = commands.last
    print(instruction) //->causes warning, please ignore

    switch instruction {
    //`"N"?` is a shortcut form of `Optional.Some("N")`
    //And `"N"` is taken as a Character
    case "N"?:
        self.usersRobot.yPosition = self.usersRobot.yPosition + 1
    case "S"?:
        self.usersRobot.yPosition = self.usersRobot.yPosition - 1
    case "E"?:
        self.usersRobot.xPosition = self.usersRobot.xPosition + 1
    case "W"?:
        self.usersRobot.xPosition = self.usersRobot.xPosition - 1
    case "P"?:
        pickUpGummyBears()
    case "D"?:
        if self.usersRobot.numberOfBagsHeld > 0 {
            dropGummyBears()
        } else {
            print("THE ROBOT ISNT HOLDING ANY BAGS CURRENTLY")
        }
    case nil:
        print("commands is emptry")
    default:
        print("WASNT RECOGNISED CHARACTER")
    }
}