0

I'm having trouble converting / understanding how Obj-C completion handlers work, and how I can convert them to Swift 2.0... Specifically, here is the code:

[BLAH aRandomTaskWithURL:NSURL completion:^(NSURL *compVar) {

//do something with compVar

}];

here is my attempt:

BLAH.aRandomTaskWithURL(myURL,completion: (compVar:NSURL)  ) {

            print(compVar)
   }

the above produces the error "delete compVar:" ... so i delete it, and then it says "cannot invoke type (NSURL) with argument list of type ' ( () -> () )'

I've tryed several times to define the Swift 2.0 equiv of compVar, but to no luck... I've also read ( and followed along with ) the relevant documentation on Swift completion variables, again to no luck. What am I missing?

But, when I add "Void in"

BLAH.aRandomTaskWithURL(myURL, completion:  ) { Void in

            //do something
    }

there is no error, but I don't have access to what should be the completion variable.

here is the actual obj-c code ( i was just trying to keep it general before ):

(void)optimalGIFfromURL:(NSURL*)videoURL loopCount:(int)loopCount completion:(void(^)(NSURL *GifURL))completionBlock {
1
  • Update your question with your best Swift code and point out what issue you are having. Commented Jan 30, 2016 at 0:27

1 Answer 1

1

It is either:

BLAH.aRandomTaskWithURL(myURL, completion: { compVar: NSURL in
    print(compVar)
})

Or, using trailing closure syntax:

BLAH.aRandomTaskWithURL(myURL) { compVar: NSURL in
    print(compVar)
}

FYI, it also depends upon the nullability configuration of the NSURL parameter. If nullability hasn't been specified, it would be:

BLAH.aRandomTaskWithURL(myURL) { compVar: NSURL! in
    print(compVar)
}

Or, if it was explicitly marked as nullable:

BLAH.aRandomTaskWithURL(myURL) { compVar: NSURL? in
    print(compVar)
}

Or, easiest, you can just have the compiler infer the nullability:

BLAH.aRandomTaskWithURL(myURL) { compVar in
    print(compVar)
}

If you've properly imported the headers for aRandomTaskWithURL, code completion will show you the correct configuration of the compVar (whether optional or not).

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

4 Comments

hmm, still getting error, so i looked up the actual function, and i updated my answer with it
@säculnój - Your example is BLAH.aRandomTaskWithURL(myURL, completion: ) { ... }. that closing parenthesis is in the wrong place. You effectively said "ok, here's completion", but then didn't provide it. It's either BLAH.aRandomTaskWithURL(myURL, completion: { compVar in ... }) (note, closing parenthesis after the closing brace) or BLAH.aRandomTaskWithURL(myURL) { compVar in ... } (note, no completion parameter specified at all, at which point closing parenthesis is before the braces of the trailing closure.
ok, so defining compVar outside the closure and then using your first suggestion fixed it.. but now im actually more confused than before lol. basically, how does my function know to use compVar?
It's logically equivalent to the Objective-C block pattern where you call aRandomTaskWithURL supplying it a URL and a block (called a "closure" in Swift) and that method, when it's done, will call your block/closure, providing you some URL you can reference inside your block/closure. If you're used to using completion blocks in Objective-C, it works the same in Seift with completion closures (but the syntax is just slightly different, and, IMHO, a little less cryptic).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.