Skip to content

Migration guide from Objective C to Swift API Client

Maz Jaleel edited this page Jun 30, 2016 · 10 revisions

This guide explains how to migrate your code from the Objective-C API Client to the Swift API Client.

If you have any issue, please contact our team at [email protected].

Table of contents

  1. Migrate your code to Swift API Client
  2. Where to find the Swift API Client?
  3. Initialization
  4. Class name
  5. Callback conventions
  6. FAQ
  7. When should I use the Swift API Client?
  8. When should I not migrate to the Swift API Client?
  9. Can I still use the Objective-C API Client?

Migrate your code to Swift API Client

Where to find the Swift API Client?

The latest version can always be found on GitHub and CocoaPods.

Objective-C API Client

pod 'AlgoliaSearch-Client', '~> 3.5'

Swift API Client

use_frameworks!
pod 'AlgoliaSearch-Client-Swift', '~> 1.4'

Initialization

Objective-C API Client

#import "ASAPIClient.h"

ASAPIClient *apiClient = [ASAPIClient apiClientWithApplicationID:@"YourApplicationID" apiKey:@"YourAPIKey"];

Swift API Client

#import "AlgoliaSearch-Swift.h"

Client *apiClient = [[Client alloc] initWithAppID:@"YourApplicationID" apiKey:@"YourAPIKey"];

Class name

Objective-C API Client Swift API Client
ASAPClient Client
ASRemoteIndex Index
ASQuery Query
ASBrowseIterator BrowseIterator

Callback conventions

Objective-C API Client

[index search:query success:^(ASRemoteIndex *index, ASQuery *query, NSDictionary *result) {
    // no error
} failure:^(ASRemoteIndex *index, ASQuery *query, NSString *errorMessage) {
    // error
}];

Swift API Client

[index search:query completionHandler:^(NSDictionary * content, NSError * error) {
    // error `nil` or `NSError` object
    // content contains the results if no error
}];

FAQ

When should I use the Swift API Client?

If your project is in Swift or partially in Swift, you should use the Swift API Client. We don't support Swift in our Objective-C API Client.

When should I not migrate to the Swift API Client?

If your project is Objective-C only and you don't plan to use Swift, it is maybe better to continue to use the Objective-C API Client.

Also, you should know that query.queryType, query.removeWordsIfNoResult and query.typoTolerance are not accessible in Objective-C.

Can I still use the Objective-C API Client?

Yes. If you are using the Objective-C API Client and do not want to upgrade then you can stick to it. We will continue to update and support it.