小编mfo*_*est的帖子

Swift Optionals - 在解开可选值时意外地发现nil

我是Swift的新手,并且一直试图围绕可选值包围(ha)我的脑袋.据我所知 - 尽管我可能错了 - 变量可以是可选的,因此包含一个nil值,这些必须在代码中考虑.

每当我点击我的应用程序中的"保存"按钮时,我都会收到错误:'致命错误:在展开可选值时意外发现nil'.

@IBAction func saveTapped(sender: AnyObject) {

    //code to save fillup to Parse backend

    // if variable is not nil
    if let username = PFUser.currentUser()?.username {

        // set username equal to current user
        self.object.username = username

    }else{

        println("PFUser.currentUser()?.username contained a nil value.")

    }

    // if variable is not nil
    if let amount = self.amountTextField.text {

        //set amount equal to value in amountTextField
        self.object.amount = self.amountTextField.text

    }else{

        println("self.amountTextField.text contained a nil value.")

    }

    // if variable is …
Run Code Online (Sandbox Code Playgroud)

ios parse-platform swift

2
推荐指数
1
解决办法
1万
查看次数

标签 统计

ios ×1

parse-platform ×1

swift ×1