简体   繁体   中英

Unable to convert string… to string in Swift

I'm trying to make a simple debuger in a view.. and I have the following: 在此输入图像描述

I'm using Alamofire to make the request, receiving the data with responseString and then sending it with App.log( response ) towards debugViewController 's log method which as you can see expects a string as well.

Now, trying to compile this returns an error which is very weird for me as I'm new to Swift. Cannot convert string to the argument type expected in debugViewController.log() which is in fact String as well ?

Please enlight me on this one.

Here you have the debugViewController :

import UIKit

class debugViewController: UIViewController {

    @IBOutlet weak var debugTextField: UITextView!

    @IBAction func dismissDebug(sender: AnyObject) {
        self.dismissViewControllerAnimated(true, completion: nil)
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

     func log( data: String ) {
        debugTextField.text = data
    }

}

And here you can see how I make the call and send data:

Alamofire.request( .POST, API_URL, parameters: [ "action": "authenticate", "email": userEmail!, "password": userPassword! ] )
            .responseString { response in

                guard let value = response.result.value else
                {
                    return App.alert( "error", message: "Did not receive data")
                }

                guard response.result.error == nil else
                {
                    print( response.result.error )
                    return App.alert( "error", message: "An error occurred" )
                }

                App.log ( value )


        }

debugViewController is a class (I advise you to start class names with a capital letter) and you are trying to invoke an instance method on the class itself, hence the error (as it actually expects an instance of type debugViewController ).

You should keep an instance of the debugViewController after it is being created so you could call the log method on the instance rather than on the class

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM