0

Slowly getting into Swift but still struggling with the completion blocks. How would the following code look like in Swift?

[self.eventStore requestAccessToEntityType:type completion:^(BOOL granted, NSError *error) {
    dispatch_async(dispatch_get_main_queue(), ^{
        [self alertViewWithDataClass:((type == EKEntityTypeEvent) ? Calendars : Reminders) status:(granted) ? NSLocalizedString(@"GRANTED", @"") : NSLocalizedString(@"DENIED", @"")];            
    });
}];

2 Answers 2

3
self.eventStore.requestAccessToEntityType(type) {
    (granted: Bool, err: NSError!) in
    dispatch_async(dispatch_get_main_queue()) {
        ...
    }
}

for an example of working code, I was experimenting with this exact API in swift :)

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

Comments

2

Your Objective-C 'completion blocks' in Swift (now called 'closures' in this context) will contain all of the same information:

  1. parameter labels and types (at the start of the block in parentheses)
  2. return type (preceded by '->')
  3. the keyword 'in' separating the signature from the code

Note that the signature of the method specifies the type for the parameters, so all you really need to do there is supply names for them :) (type inference FTW!) Additionally, your block returns 'Void' so we don't need to include the return type here, either.

That would give us:

self.eventStore.requestAccessToEntityType(type) { (granted, err) in
    dispatch_async(dispatch_get_main_queue()) {
        ...other stuff...
    }
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.