简体   繁体   English

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

[英]Binary operator '+' cannot be applied to operands of type 'String' and '() -> String'

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 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 VC_VerifyPhone.swift

On the 2nd last line of the 2nd class, it shows the error: 在第二个类的倒数第二行上,显示错误:

Binary operator '+' cannot be applied to operands of type 'String' and '() -> String' 二进制运算符'+'不能应用于'String'和'()-> String'类型的操作数

What am I doing wrong and how can I fix it? 我在做什么错,我该如何解决?

EDIT: 编辑:

Now I'm getting this error: 现在我得到这个错误:

Instance member 'gettoken' cannot be used on type 'ViewController'; 实例成员“ gettoken”不能在“ ViewController”类型上使用; did you mean to use a value of this type instead? 您的意思是改用这种类型的值吗?

You've been bitten by a bit of Swift fun/weirdness/etc. 你被Swift的乐趣/怪异/等等所吸引。 depending on your point of view! 根据您的观点!

gettoken() is an instance method of the ViewController class and should be called on an instance. gettoken()ViewController类的实例方法,应在实例上调用。 Eg something like: 例如:

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

You have not done this, instead you've called it on the class itself which isn't the same thing at all. 您尚未完成此操作,而是在本身上调用了它,这根本不是一回事。 You need an instance of your ViewController class on which to call get token() . 您需要ViewController类的实例 ,在该实例上调用get token()

Why the confusing error? 为什么会造成混乱的错误?

For reasons I won't attempt to explain Swift 4 (at least, Swift is a moving target) allows you to write: 由于某些原因,我不会尝试解释Swift 4(至少,Swift是一个移动的目标)允许您编写:

myViewController.gettoken()

as

ViewController.gettoken(myViewController)()

And this is partly what you wrote and the error messages refers to this form. 这部分是您编写的内容,并且错误消息引用了此表单。 (For the terminally curious think of instance methods having a type of the form class -> arguments -> result and being members of the class.) (对于最终好奇的实例方法,其实例类型的类型为class-> arguments-> result并且是该类的成员。)

You need to access the very instance of ViewController . 您需要访问ViewController实例。 which invoked the VC_VerifyPhone . 它调用了VC_VerifyPhone

You may need to pass self when opening the VC_VerifyPhone or some closure returning the same value as gettoken() or there may be some other ways. 当您打开VC_VerifyPhone或某些闭包返回与gettoken()相同的值时,可能需要传递self ,或者可能有其他方法。 But anyway you need to pass some info from ViewController to VC_VerifyPhone . 但是无论如何,您都需要将一些信息从ViewController传递到VC_VerifyPhone

Why don't you pass the token directly? 您为什么不直接传递令牌?

In your UIViewController : 在您的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)
    }
}

In VC_VerifyPhone : 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`")
    }
}

The way that your code is structured requires you to have an instance of ViewController , which has its token property set. 代码的结构方式要求您有一个ViewController实例,该实例的token属性已设置。 Once you have that, then you can substitute viewController.getToken() for ViewController.gettoken(ViewController) . 一旦有了,就可以用viewController.getToken()代替ViewController.gettoken(ViewController)

You first need a view controller with a token. 您首先需要一个带有令牌的视图控制器。 For example: 例如:

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

Then, you can call controller.getToken() and interpolate that into your string, which looks like this: 然后,您可以调用controller.getToken()并将其插值到您的字符串中,如下所示:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 二元运算符“+”不能应用于“_”和“String”类型的操作数 - Binary operator '+' cannot be applied to operands of type '_' and 'String' 二元运算符“+”不能应用于“字符串”和“字符串?”类型的操作数 - Binary operator '+' cannot be applied to operands of type 'String' and 'String?' 二元运算符“==”不能应用于“[String]”和“String”类型的操作数 - Binary operator '==' cannot be applied to operands of type '[String]' and 'String 二进制运算符&#39;〜=&#39;不能应用于&#39;String&#39;和&#39;String?&#39;类型的操作数 - Binary operator '~=' cannot be applied to operands of type 'String' and 'String?' 二进制运算符“ +”不能应用于类型为“字符串”和“字符串感叹号”的操作数 - Binary operator '+' cannot be applied to operands of type 'String' and 'String exclamationmark 二进制运算符&#39;==&#39;不能应用于&#39;NSObject&#39;和&#39;String&#39;类型的操作数 - Binary operator '==' cannot be applied to operands of type 'NSObject' and 'String' 二进制运算符-不能应用于字符串类型的操作数! 和诠释 - Binary operator - cannot be applied to operands of type String! and Int 二进制运算符&#39;==&#39;不能应用于&#39;Any?&#39;类型的操作数 和&#39;String&#39;Swift iOS - Binary operator '==' cannot be applied to operands of type 'Any?' and 'String' Swift iOS 二进制运算符&#39;==&#39;不能应用于类型&#39;UIImage?&#39;的操作数 和“字符串”-Swift 4 - Binary operator '==' cannot be applied to operands of type 'UIImage?' and 'String' - Swift 4 Swift-二进制运算符&#39;&gt; =&#39;不能应用于类型为&#39;String&#39;和&#39;Int&#39;的操作数 - Swift - Binary operator '>=' cannot be applied to operands of type 'String' and 'Int'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM