2

I'm trying to make a simple debuger in a view.. and I have the following: enter image description here

I'm using Alamofire to make the request, receiving the data with responseString and then sending it with App.log( response ) towards debugViewController's logmethod 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 )


        }
5
  • 2
    how is debugViewController defined? it seems you are invoking an instance method on the class itself Commented Oct 15, 2015 at 12:39
  • @giorashc updated my question. Commented Oct 15, 2015 at 12:41
  • Can you post the code where data is assigned a value? Also, check if data is not an optional. If it is then try unwrapping it like data! Commented Oct 15, 2015 at 12:43
  • If you're using response.result, it is of type anybject. Have you typecasted it to string. While sending it to App.log() ? Commented Oct 15, 2015 at 12:43
  • @iamyogish i have updated my question with the response part. Commented Oct 15, 2015 at 12:46

1 Answer 1

3

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

Sign up to request clarification or add additional context in comments.

10 Comments

Ok, so in layman terms what I was doing wrong was that I was calling a dynamic method on a non-instantiated class right ? I mean, I don't have Swift background but I know some OOP. Regarding the camelCase stuff, I've just read that it's better to use camelCase pattern, but, seems like i got it wrong. Anyway, why is it if i change the func to static, I can't access the debugTextField in log method anymore ?
Yes @giorashc is right. You're calling an instance method of debugViewController. You can either create an instance or make the method log method as class method.
@edduvs You can't access the debugTextField in log method because, it's a property of an debugViewController class. And to access a property of a class you need to create an instance.
@iamyogish if I make the log as class method, I can't access the value from IBOutlet anymore ( debugTextField ). And now, it says unexpectedly found nil while unwrapping an Optional value, even though the string contains my JSON response. What's to be done ?
@edduvs Yes when you're calling the class functions you'll not be able to access the IBOutlets, because outlets are associated with the instance. And now "unexpectedly found nil while unwrapping an Optional value" this is because you're trying to set the text of the textfield which is an outlet. And textfield is nil because you've not created a instance of the class.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.