[Swift] Extra argument ‘completion’ in call

xcodeIn iOS 8 SDK Development, the use of a closure to act on a response from the user when we ask them to use their twitter account produces the following compilation error:

Extra argument 'completion' in call

The error is misleading because there is no “extra argument” in the call to “accountStore.requestAccessToAccountsWithType“. Below the problem is on line #6. See it?

func reloadTweets() {
  let accountStore = ACAccountStore()
  let twitterAccountType = accountStore.accountTypeWithAccountTypeIdentifier(ACAccountTypeIdentifierTwitter)
  accountStore.requestAccessToAccountsWithType(twitterAccountType, options: nil, completion:
    { (granted: Bool, error: NSError) -> Void in
      if (!granted) {
        println("account access not granted")
      } else {
        println("account access granted")

It does not stand out and I only figured it out because I recognized the closure for ‘completion:‘ is a type-alias to ‘ACAccountStoreRequestAccessCompletionHandler‘, which is documented as:

typealias ACAccountStoreRequestAccessCompletionHandler = (Bool, NSError!) -> Void

Adding the exclamation mark (!) after NSError to force unwrap solve the error. The confusing part to me is NSError is not an optional type (at least it is not clear in the definition of NSError and I am not too familiar with Objective-C). Why does it need to be forced unwrapped? Would appreciate any further explanation on this from anyone watching. 🙂

This entry was posted in Programming, Swift. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s