Behrouz Riahi
Behrouz Riahi

Reputation: 1791

Error while invoking performSegue function

I could call my performSegue outside alamofire validation handler. but I want to segue after the alamofire request is succeeded

here is my code

Alamofire.request("link", method: .post, parameters: parameters!, encoding: JSONEncoding.default).responseJSON { response in
                switch response.result {
                case .success:
                    performSegue(withIdentifier: "successRegistration", sender: Any?)
                case .failure(let error):
                    print(error)
                }
            }

the problem is in the following line

performSegue(withIdentifier: "successRegistration", sender: Any?)

it shows me the following error

expected member name or constructor call after type name

and xcode gives me two fixes, one is to change Any? to Any?() which gives me another error as shown below:

cannot invoke initializer for type 'Any?' with no arguments

the other fix is to change Any? to Any?.self and this gives me another error show below:

Implicit use of 'self' in closure, use 'self.' to make capture semantics explicit

the same problem (above) occurs when I change Any? to nil

What is causing this error? (knowing that the performSegue function works fine outside Alamofire when I change Any? to nil )

Upvotes: 1

Views: 525

Answers (3)

Paulo Mattos
Paulo Mattos

Reputation: 19339

Try this:

self.performSegue(withIdentifier: "successRegistration", sender: nil)

Some notes:

  • inside a closure, which you are, you always need to use self to reference the outer object. In your case, that would be the UIViewControlller.

  • Any is a type name (a protocol) and not a value. To specify "empty" values, use nil instead. Use Any as a type for general values, with no defined/known type.

Upvotes: 0

Temirlan
Temirlan

Reputation: 192

Why do you pass "Any?" as argument. It's similar to data type like Int or Bool. If you want pass nothing just send nil.

Upvotes: 1

Ruhi
Ruhi

Reputation: 176

self.performSegueWithIdentifier("successRegistration", sender: nil)

try to use "self" before performSegue function.

Upvotes: 4

Related Questions