可选类型'String?'的值没有包装;你的意思是使用'!'或者 '?'在Swift 2.0中使用解析 [英] Value of optional type 'String?' not unwrapped; did you mean to use '!' or '?' Using Parse In Swift 2.0

查看:71
本文介绍了可选类型'String?'的值没有包装;你的意思是使用'!'或者 '?'在Swift 2.0中使用解析的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

抱歉,所有这些都可以,但可以帮忙.实现它可能是一个非常简单的解决方案,但这是我麻烦的代码:

Sorry new to all this, but could do with a hand. Realise its probably a very simple solution, but here is my troublesome code:

@IBAction func loginAction(sender: AnyObject) {

    var username = self.usernameField.text
    var password = self.passwordField.text

    if (count(username.utf16) < 4 || count(password.utf16) < 5) {

        var alert = UIAlertView(title: "Invalid", message: "Username must be greater then 4 and Password must be greater then 5", delegate: self, cancelButtonTitle: "OK")
        alert.show()

    }else {

        self.actInd.startAnimating()

        PFUser.logInWithUsernameInBackground(username, password: password, block: { (user, error) -> Void in

            self.actInd.stopAnimating()

            if ((user) != nil) {

                var alert = UIAlertView(title: "Success", message: "Logged In", delegate: self, cancelButtonTitle: "OK")
                //alert.show()

                self.navigationController!.popToRootViewControllerAnimated(false)

            }else {

                var alert = UIAlertView(title: "Error", message: "\(error)", delegate: self, cancelButtonTitle: "OK")
                alert.show()

错误照片:可选类型'String?'的值没有包装;你的意思是使用'!'或者 '?'

推荐答案

错误消息几乎是不言自明的,您需要打开可选选项.出口是可选的,因此usernameField.text和passwordField.text都返回String类型? (类型是可选字符串,而不是字符串),因此除非将其解包为String,否则您将无法执行任何与字符串相关的操作.这是您可以做到的

The error message is pretty much self-explanatory, you need to unwrap the optionals. outlets are optionals so both usernameField.text and passwordField.text return a type String? (The type is optional string, not string) so you can't do any string related unless you unwrap it to be String. here is how you can do that

if  let username = self.usernameField.text, password = self.passwordField.text {
    //your code
}

这篇关于可选类型'String?'的值没有包装;你的意思是使用'!'或者 '?'在Swift 2.0中使用解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆