How do I use a completionBlock in swift? - closures

I'm trying to utilize swift's SKStoreProductViewController, but am getting errors with my syntax, specifically with my completion block.
Here is my code:
let storeViewController:SKStoreProductViewController = SKStoreProductViewController();
storeViewController.delegate = self;
var productparameters = [SKStoreProductParameterITunesItemIdentifier:someitunesid];
storeViewController.loadProductWithParameters(productparameters,
(success: Bool!, error: NSError!) -> Void in
if success {
self.presentViewController(storeViewController, animated: true, completion: nil);
} else {
NSLog("%#", error)
}
)
After running this I get an expected "," separator error between the error:NSError!),-> Void
This doesn't make sense to me as the apple docs call for:
func loadProductWithParameters(_ parameters: [NSObject : AnyObject]!,
completionBlock block: ((Bool, NSError!) -> Void)!)
What am I doing wrong?

You're 99% there, you just need braces around your block to have the correct closure syntax:
storeViewController.loadProductWithParameters(productparameters, { (success: Bool!, error: NSError!) -> Void in
if success {
self.presentViewController(storeViewController, animated: true, completion: nil);
} else {
NSLog("%#", error)
}
})
You can read more about closures in Apple's documentation.

Related

Swift Block Syntax with Objective-C Function [Venmo-iOS-SDK]

I'm currently trying to use the Venmo-iOS-SDK for an application I am working on. The SDK is in objective-C, while I'm trying to use it with a swift app.
I'm having trouble translating the syntax of a completion obj-c block to swift. I found sample code implementing a function I want to use.
- (IBAction)logInButtonAction:(id)sender {
[[Venmo sharedInstance] requestPermissions:#[VENPermissionMakePayments,
VENPermissionAccessProfile]
withCompletionHandler:^(BOOL success, NSError *error) {
if (success) {
NSLog("Success")
} else {
NSLog("Failure")
}
}];
}
I've tried doing this
#IBAction func loginButtonAction(sender: AnyObject){
Venmo.sharedInstance().requestPermissions([VENPermissionMakePayments, VENPermissionAccessPhone], withCompletionHandler: { (success: Bool, error: NSErrorPointer) -> Void in
if success{
println("Yes")
}else{
println("No")
}
})
}
But get the error
"Cannot invoke 'requestsPermissions with an argument list of type
'([String], withCompletionHandler: (Bool, NSError) -> Void)'
Is this a problem with how i'm translating the block? Or something else. Looking at the Venmo-SDK the obj-C functions are defined like this
- (void)requestPermissions:(NSArray *)permissions withCompletionHandler:(VENOAuthCompletionHandler)handler;
and
- (void)requestPermissions:(NSArray *)permissions withCompletionHandler:(VENOAuthCompletionHandler)handler;
You can write it like this (note the lack of types on the completion handler params):
#IBAction func loginButtonAction(sender: AnyObject) {
Venmo.sharedInstance().requestPermissions([VENPermissionMakePayments, VENPermissionAccessPhone], withCompletionHandler: { (success, error) -> Void in
// code here
})
}
A bit more concise with Swift 2 syntax would be omitting the -> Void and explicit withCompletionHandler: parameter:
#IBAction func loginButtonAction(sender: AnyObject) {
Venmo.sharedInstance().requestPermissions([VENPermissionMakePayments, VENPermissionAccessPhone]) { (success, error) in
// code here
}
}
You'll also want to make sure you change your println statements to print.

Swift closure: cannot invoke a function with its argument list

I am writing a Swift function using closure. A should-be-compilable code sample is like this,
import Foundation
typealias PKSynchronizeProgressBlock = (Double) -> Void
typealias PKSynchronizeCompletionBlock = (Bool, NSError?) -> Void
class X {
func synchronizeAppDataWithProgress(
progress: PKSynchronizeProgressBlock?,
completion: PKSynchronizeCompletionBlock?) {
dispatch_async(dispatch_get_main_queue(), {
// Do a lot of downloading, and during the process
// {
// If progress is updated
if (progress != nil) {
progress!(Double(0))
}
//
// If something goes wrong
if (completion != nil) {
completion!(false, nil)
}
// }
dispatch_async(dispatch_get_main_queue(), {
if (completion != nil) {
completion!(true, nil)
}
})
})
}
func foo() {
self.synchronizeAppDataWithProgress({ (progress: Double) -> Void in
self.launchProgressBar.progress = progress
}, completion: { (success: Bool, error: NSError?) -> Void in
if success {
self.launchProgressBar.progress = 1.0
}
else {
print("Failed to synchronize app data with error %#", error!)
}
})
}
}
However, this code does not compile. Xcode says that
cannot invoke 'synchronizeAppDataWithProgress' with an argument list
'(progress: (Double) -> Void, completion: (Bool, NSError?) -> Void)'
What should I do? Did I make any stupid mistake in my code?
Update:
Thanks to #Mario Zannone. I fixed the first two mistakes in my code above. That was: (1) I inserted a redundant progress: in the function call. I have removed that. (2) I updated UI in a thread other than main thread.
But the code still does not work if I don't comment out the following single line in the foo(),
self.launchProgressBar.progress = progress
Do you have any clue why?
Xcode can be picky sometimes with the way arguments are listed inside closures. I've found it best to leave the type inferred. Also be sure to use capture lists to avoid strong reference cycles in your closures.
Using the Alamofire dependency, I've rewritten your code above and it compiles.
import Alamofire
typealias ProgressBlock = (Double) -> Void
typealias CompletionBlock = (Bool, ErrorType?) -> Void
class ExampleDataSource {
func fetchData(progress: ProgressBlock?, completion: CompletionBlock?) {
// Here we use the Alamofire Dependency for progress reporting simplicity.
Alamofire.request(.GET, "https://www.myexampledomain.com")
.progress { bytesRead, totalBytesRead, totalBytesExpectedToRead in
// bytesRead, totalBytesRead, and totalBytesExpectedToRead are Int64
// so we must perform unit conversion
let progressPercentage = Double(totalBytesRead) / Double(totalBytesExpectedToRead)
// here we optionally call the ProgressBlock 'progress' and pass it the progressPercentage
progress?(progressPercentage)
}
.response { request, response, data, error in
// here we usually parse the data, but for simplicity we'll
// simply check to see if it exists.
let completionFlag = (data != nil)
// note that NSError? is interchangable with ErrorType?
completion?(completionFlag, error)
}
}
func performTasks() {
// first we'll set up our closures...
let progressBlock: ProgressBlock = { progress in
// here we update the UI or whatever
// the nice thing about the Alamofire dependency is
// that it's thread-safe :]
}
let completionBlock: CompletionBlock = { success, error in
// here we do whatever we need to do when the
// network operation finishes, or handle the
// errors appropriately
}
// then we'll pass them into our fetchData method
fetchData(progressBlock, completion: completionBlock)
}
}

The significance of the Bool value of PFUser signUpInBackgroundWithBlock

I am wondering what is the significance of the succeeded Bool value in the function block? As I have seen sample codes from parse ignores it and only check if error is nil or not.
If the bool value is not required, why is it there in the first place?
user.signUpInBackgroundWithBlock({ (succeeded: Bool, error: NSError?) -> Void in
if error == nil {
if succeeded { // IS THIS REQUIRED AT ALL??
}
}
else {
}
});
Or we could just do this?
user.signUpInBackgroundWithBlock({ (succeeded: Bool, error: NSError?) -> Void in
if error == nil {
//Do something
}
else {
}
});

Parse SDK methods not working in Xcode 6.3 Beta

So far I am having issues with blocks like this:
user.signUpInBackgroundWithBlock {
(succeeded: Bool!, error: NSError!) -> Void in
if error == nil {
println("success")
} else {
println("\(error)");
// Show the errorString somewhere and let the user try again.
}
}
When I add this into Xcode I get this:
Cannot invoke 'signUpInBackgroundWithBlock' with an argument list of type '((Bool!, NSError!) -> Void)'
When I run this code in Xcode 6.3 (non beta) it works fine. But in the Beta it fails and wont allow me to build. Any ideas if this will be cleared up or if there is a different implementation that I could use. Ive tried using just the signUpInBackgroundWithTarget but Im just not able to access the error correctly if one is received.
be sure you are using SDK version 1.7.1, then removing the types from your closure should do the trick:
user.signUpInBackgroundWithBlock { (succeeded, error) -> Void in
if error == nil {
println("success")
} else {
println("\(error)");
// Show the errorString somewhere and let the user try again.
}
}
Due to the new addition of "Nullability Annotations" to Swift 1.2, you have to rewrite the code above like this (using Parse 1.7.1+):
user.signUpInBackgroundWithBlock { (succeeded: Bool, error: NSError?) -> Void in
if let error = error {
println(error) // there is an error, print it
} else {
if succeeded {
println("success")
} else {
println("failed")
}
}
}
Parse is now returning optionals (?) instead of explicitely unwrapped objects (!).
Notation of Swift is changed
class AAPLList : NSObject, NSCoding, NSCopying {
// ...
func itemWithName(name: String!) -> AAPLListItem!
func indexOfItem(item: AAPLListItem!) -> Int
#NSCopying var name: String! { get set }
#NSCopying var allItems: [AnyObject]! { get }
// ...
}
After annotations:
class AAPLList : NSObject, NSCoding, NSCopying {
// ...
func itemWithName(name: String) -> AAPLListItem?
func indexOfItem(item: AAPLListItem) -> Int
#NSCopying var name: String? { get set }
#NSCopying var allItems: [AnyObject] { get }
// ...
}
So you can change
(succeeded: Bool!, error: NSError!) -> Void in
to
(success: Bool, error: NSError?) -> Void in
Which Parse SDK are you using? They released version 1.7.1 a few days ago that should fix your issue.
Change:
(succeeded: Bool!, error: NSError!) -> Void in
to
(succeeded, error) -> Void in
This change is required due to changes in the Parse SDK

Swift Completion Handlers in GameKit

How can I implement the following in Swift?
func findMatchForRequest(_ request: GKMatchRequest!,
withCompletionHandler completionHandler: ((GKMatch!,
NSError!) -> Void)!)
When I tried
var request = GKMatchRequest();
request.minPlayers = 2;
request.maxPlayers = 4;
GKMatchmaker.sharedMatchmaker().findMatchForRequest(request, withCompletionHandler: { (match : GKMatch!, error: NSError!) -> Void in {
}
})
it gives a "can not convert the expression type" compilation error. Am I missing something obvious?
You're close. You just have an unnecessary set of braces in your completion handler. Here's the correct version:
GKMatchmaker.sharedMatchmaker().findMatchForRequest(
request,
withCompletionHandler: {(match : GKMatch!, error: NSError!) -> Void in
NSLog("This works")
})
EDIT: I meant braces!
FYI you could use trailing closure:
GKMatchmaker.sharedMatchmaker().findMatchForRequest(request) {
match, error in
println("This works")
}

Resources