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
Ios swift中的UITextfield致命错误_Ios_Swift_Uitextfield - Fatal编程技术网

Ios swift中的UITextfield致命错误

Ios swift中的UITextfield致命错误,ios,swift,uitextfield,Ios,Swift,Uitextfield,在我的swift应用程序中,我正在使用parse。在将登录寄存器字段详细信息放入解析时,我遇到了类似于“致命错误:在textfield中展开可选值“”的错误。请帮帮我。我的代码如下 //CLASS 1 class login: UIViewController{ @IBAction func create_acc_but(sender: UIButton) { createaccount.shared.create_account() } } // CLASS 2 pu

在我的swift应用程序中,我正在使用parse。在将登录寄存器字段详细信息放入解析时,我遇到了类似于“致命错误:在textfield中展开可选值“”的错误。请帮帮我。我的代码如下

//CLASS 1
class login: UIViewController{ 
  @IBAction func create_acc_but(sender: UIButton) { 
    createaccount.shared.create_account() 
   }
}
// CLASS 2

public class createaccount {

let login_obj:login = login()
class var shared: createaccount {
    struct Static {
        static let instance: createaccount = createaccount()
    }
    return Static.instance
 }

func create_account() {


    var user = PFUser()

    user.username = login_obj.username_txtfd.text //receiving error in this line.
    user.email =  login_obj.email_txtfd.text
    user.password = login_obj.passwd_txtfd.text

    user.signUpInBackgroundWithBlock {
        (succeeded: Bool!, error: NSError!) -> Void in
        if error == nil {
            // Hooray! Let them use the app now.
        } else {
            let errorString = error.userInfo // error.userInfo["error"] as NSString
            println(errorString)
            // Show the errorString somewhere and let the user try again.
        }
    }
  }
 }

这一切都是关于包装和展开可选值。此错误是因为您正在尝试展开可选值。确保您的属性必须声明为可选,如下所示

@IBOutlet var txtNumber1:UITextField!
var a:Int! = txtNumber1.text.toInt()
您可以像这样访问值

@IBOutlet var txtNumber1:UITextField!
var a:Int! = txtNumber1.text.toInt()
有关可选的更多信息,请访问此链接:


这肯定会对您有所帮助

您好Indrajeet,谢谢您的回复。我在同一个类中获取textfield的值。但我是通过另一门课进入的。当时抛出的错误是“致命错误:在展开可选值时意外发现nil”。是的,如果未正确展开,则会发生此错误。试试这个user.username=login\u obj.username\u txtfd.text!