二进制运算符'+'不能应用于'String'和'()-> String'类型的操作数

时间:2018-07-08 01:51:04

标签: swift

class ViewController: UIViewController{
    var token: String?

    func settoken(newtoken: String){
        token = newtoken
    }

    func gettoken() -> String{
        return token!
    }

    func gotoverifyphone(){
        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let verifyphone = storyboard.instantiateViewController(withIdentifier: "verifyphone")
        OperationQueue.main.addOperation{
            self.present(verifyphone, animated: true, completion: nil)
        }
    }

    @IBAction func btnPressed(_ sender: Any){
        ...some code
        let newtoken = json["message"] as? String
        self.settoken(newtoken: newtoken!)
        self.gotoverifyphone()
    }
}

ViewController.swift

class VC_VerifyPhone: UIViewController {
    @IBAction func btnVerifyPressed(_ sender: Any){
        var request = URLRequest(url: url)
        request.httpMethod = "POST"
        request.addValue("application/json", forHTTPHeaderField: "Content-Type")
        let token = "Bearer " + ViewController.gettoken(ViewController)
// the line above shows Binary operator '+' cannot be applied to operands of type 'String' and '() -> String'
        request.addValue("authorization", forHTTPHeaderField: token)
    }
}

VC_VerifyPhone.swift

在第二类的第二行上,它显示错误:

二进制运算符'+'不能应用于类型为'String'和'()-> String'的操作数

我在做什么错,我该如何解决?

编辑:

现在我收到此错误:

实例成员'gettoken'不能用于'ViewController'类型;您是不是要使用这种类型的值?

3 个答案:

答案 0 :(得分:3)

您已经被Swift的乐趣/怪异/等等所吸引。根据您的观点!

gettoken()ViewController类的 instance 方法,应在实例上调用。例如:

var myViewController : ViewController = ...
...
let token = myViewController.gettoken()

您尚未执行此操作,而是在 class 本身上调用了它,这根本不是一回事。您需要您的 ViewController 类的实例,以在其上调用get token()

为什么会造成混乱的错误?

出于某种原因,我不会尝试解释Swift 4(至少,Swift是一个移动的目标)允许您编写:

myViewController.gettoken()

ViewController.gettoken(myViewController)()

这部分是您编写的内容,并且错误消息涉及此表单。 (出于最终好奇的考虑,实例方法的类型为 class-> arguments-> result 并且是该类的成员。)

答案 1 :(得分:1)

您需要访问ViewController的实例。调用了VC_VerifyPhone

打开self时可能需要传递VC_VerifyPhone,或者某些闭包返回与gettoken()相同的值,否则可能会有其他方法。但是无论如何,您都需要将一些信息从ViewController传递到VC_VerifyPhone

为什么不直接传递令牌?

在您的UIViewController中:

func gotoverifyphone(){
    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    let verifyphone = storyboard.instantiateViewController(withIdentifier: "verifyphone") as! VC_VerifyPhone
    verifyphone.passedToken = token //<- pass your token directly
    OperationQueue.main.addOperation{
        self.present(verifyphone, animated: true, completion: nil)
    }
}

VC_VerifyPhone中:

var passedToken: String?

@IBAction func btnVerifyPressed(_ sender: Any){
    var request = URLRequest(url: url)
    request.httpMethod = "POST"
    request.addValue("application/json", forHTTPHeaderField: "Content-Type")
    if let passedToken = passedToken { //Use the passed token here
        let token = "Bearer " + passedToken
        request.addValue("authorization", forHTTPHeaderField: token)
    } else {
        //This may never happen, but you may need some output while debugging.
        print("VC_VerifyPhone opened without setting `passedToken`")
    }
}

答案 2 :(得分:0)

代码的结构方式要求您拥有一个ViewController实例,该实例设置了其token属性。一旦有了这些,就可以用viewController.getToken()代替ViewController.gettoken(ViewController)

您首先需要一个带有令牌的视图控制器。例如:

let controller = ViewController()
controller.settoken(newtoken: "abc123")

然后,您可以调用controller.getToken()并将其插值到您的字符串中,如下所示:

let token = "Bearer " + controller.getToken()