### Async/Await API Method and Usage Source: https://github.com/algolia/algoliasearch-client-swift/blob/main/AGENTS.md Shows the async method signature for search and its usage within a Task. All API methods are async, and the example demonstrates calling client.search with try await. ```swift // All API methods are async func search(params: SearchParams) async throws -> SearchResponse // Usage Task { let response = try await client.search(params) } ``` -------------------------------- ### Install CocoaPods dependency Source: https://github.com/algolia/algoliasearch-client-swift/blob/main/README.md Run this command after adding the pod to your Podfile to install the dependency. ```bash $ pod update ``` -------------------------------- ### CocoaPods installation for Objective-C and Swift API Clients Source: https://github.com/algolia/algoliasearch-client-swift/wiki/Migration-guide-from-Objective-C-to-Swift-API-Client CocoaPods installation commands for the Objective-C and Swift API Clients. The Swift client requires use_frameworks! and the pod name 'AlgoliaSearch-Client-Swift'. ```ruby pod 'AlgoliaSearch-Client', '~> 3.5' ``` ```ruby use_frameworks! pod 'AlgoliaSearch-Client-Swift', '~> 1.4' ``` -------------------------------- ### Error Handling with AlgoliaError and do-catch Source: https://github.com/algolia/algoliasearch-client-swift/blob/main/AGENTS.md Defines the AlgoliaError enum with cases for HTTP errors, unreachable hosts, and decoding errors. The usage example shows a do-catch block that switches on the error to handle each case. ```swift // Swift error types enum AlgoliaError: Error { case httpError(statusCode: Int, message: String) case unreachableHosts case decodingError(Error) } // Usage with do-catch do { let response = try await client.search(params) } catch let error as AlgoliaError { switch error { case .httpError(let code, let message): // Handle HTTP error case .unreachableHosts: // Handle network error } } ``` -------------------------------- ### Use Algolia Swift client to save and search Source: https://github.com/algolia/algoliasearch-client-swift/blob/main/README.md Import the Algolia Core and Search packages, create a client, save an object, wait for indexing, and perform a search with typo tolerance. Replace the placeholder app ID, API key, index name, and query with your own values. ```swift #if canImport(AlgoliaCore) import AlgoliaCore #endif import AlgoliaSearch let client = try SearchClient(appID: "YOUR_APP_ID", apiKey: "YOUR_API_KEY") // Add a new record to your Algolia index let response = try await client.saveObject( indexName: "", body: ["objectID": "id", "test": "val"] ) // Poll the task status to know when it has been indexed try await client.waitForTask(with: response.taskID, in: "") // Fetch search results, with typo tolerance let response: SearchResponses = try await client .search(searchMethodParams: SearchMethodParams(requests: [SearchQuery.searchForHits(SearchForHits( query: "", hitsPerPage: 50, indexName: "" ))])) ``` -------------------------------- ### Build Carthage dependencies for Algolia client Source: https://github.com/algolia/algoliasearch-client-swift/blob/main/README.md Run these commands from the project directory (for v.8.0+) to build the Carthage dependencies. The carthage-prebuild script is required for the Algolia client. ```shell carthage update ./Carthage/Checkouts/algoliasearch-client-swift/carthage-prebuild carthage build ``` -------------------------------- ### Build and Test Commands for Swift Client Source: https://github.com/algolia/algoliasearch-client-swift/blob/main/AGENTS.md Lists the yarn CLI commands for building, generating/running CTS tests, playground, and formatting, plus swift build/test commands from the client directory. ```bash # From repo root (api-clients-automation) yarn cli build clients swift # Build Swift client yarn cli cts generate swift # Generate CTS tests yarn cli cts run swift # Run CTS tests yarn cli playground swift search # Interactive playground yarn cli format swift clients/algoliasearch-client-swift # From client directory (requires Xcode/Swift toolchain) cd clients/algoliasearch-client-swift swift build # Build package swift test # Run tests ``` -------------------------------- ### Add Algolia Swift client to Podfile Source: https://github.com/algolia/algoliasearch-client-swift/blob/main/README.md Add the Algolia Swift client to your Podfile. The commented line for InstantSearchClient is not yet released for Swift 5.9. ```ruby pod 'AlgoliaSearchClient', '~> 9.0.0' # pod 'InstantSearchClient', '~> 7.0' // Swift 5.9 NOT YET RELEASED ``` -------------------------------- ### Initialization for Objective-C and Swift API Clients Source: https://github.com/algolia/algoliasearch-client-swift/wiki/Migration-guide-from-Objective-C-to-Swift-API-Client Initialization code for the Objective-C and Swift API Clients. The Swift client uses the 'Client' class and the 'initWithAppID:apiKey:' initializer. ```objc #import "ASAPIClient.h" ASAPIClient *apiClient = [ASAPIClient apiClientWithApplicationID:@"YourApplicationID" apiKey:@"YourAPIKey"]; ``` ```objc #import "AlgoliaSearch-Swift.h" Client *apiClient = [[Client alloc] initWithAppID:@"YourApplicationID" apiKey:@"YourAPIKey"]; ``` -------------------------------- ### Check Git Remote Source: https://github.com/algolia/algoliasearch-client-swift/blob/main/AGENTS.md Run this command to verify you are in the correct repository. The expected output should show 'origin .../algolia/api-clients-automation.git' to proceed; otherwise, stop. ```bash git remote -v ``` -------------------------------- ### Platform Availability: Check for newer APIs with #available Source: https://github.com/algolia/algoliasearch-client-swift/blob/main/AGENTS.md Shows how to conditionally use newer APIs by checking platform availability (iOS 15.0, macOS 12.0) with a fallback for older systems. ```swift // Check availability for newer APIs if #available(iOS 15.0, macOS 12.0, *) { // Use newer API } else { // Fallback } ``` -------------------------------- ### Async Context: Using await in Task or async function Source: https://github.com/algolia/algoliasearch-client-swift/blob/main/AGENTS.md Shows the correct way to call async client methods. The first line is incorrect because await is used outside an async context. Wrap the call in a Task or place it inside an async function. ```swift // WRONG - can't use await outside async context let response = try await client.search(params) // CORRECT - wrap in Task Task { let response = try await client.search(params) } // Or use async function func myAsyncFunction() async throws { let response = try await client.search(params) } ``` -------------------------------- ### Callback conventions for search in Objective-C and Swift API Clients Source: https://github.com/algolia/algoliasearch-client-swift/wiki/Migration-guide-from-Objective-C-to-Swift-API-Client Callback conventions for search requests. The Objective-C client uses separate success and failure blocks, while the Swift client uses a single completionHandler with an NSError parameter. ```objc [index search:query success:^(ASRemoteIndex *index, ASQuery *query, NSDictionary *result) { // no error } failure:^(ASRemoteIndex *index, ASQuery *query, NSString *errorMessage) { // error }]; ``` ```objc [index search:query completionHandler:^(NSDictionary * content, NSError * error) { // error `nil` or `NSError` object // content contains the results if no error }]; ``` -------------------------------- ### Add Algolia Swift client to Package.swift Source: https://github.com/algolia/algoliasearch-client-swift/blob/main/README.md Add the Algolia Swift client as a dependency in your Package.swift file. The version range is from 9.0.0 up to but not including 10.0.0. ```swift let package = Package( // 9.0.0 ..< 10.0.0 dependencies: [ .package(url: "https://github.com/algolia/algoliasearch-client-swift", from: "9.0.0") ], // ... ) ``` -------------------------------- ### Check Generation Config Patterns Source: https://github.com/algolia/algoliasearch-client-swift/blob/main/AGENTS.md Inspect config/generation.config.mjs to identify generated vs. hand-written files. Patterns without '!' are generated and must not be edited; patterns with '!' are hand-written and safe to edit. ```javascript // In generation.config.mjs - patterns WITHOUT '!' are GENERATED (do not edit) 'clients/algoliasearch-client-swift/**/Sources/**', // Generated '!clients/algoliasearch-client-swift/Sources/Core/**', // Hand-written ✓ '!clients/algoliasearch-client-swift/Sources/Search/Extra/**', // Hand-written ✓ ``` -------------------------------- ### Optionals: Unwrapping response.hits with if let, guard, and nil-coalescing Source: https://github.com/algolia/algoliasearch-client-swift/blob/main/AGENTS.md Demonstrates three common patterns for safely unwrapping optional values: optional binding with if let, early exit with guard, and optional chaining with a default value using ??. ```swift // Use optional binding if let hits = response.hits { // hits is non-optional here } // Or guard guard let hits = response.hits else { return } // Optional chaining let count = response.hits?.count ?? 0 ``` -------------------------------- ### Add Algolia Swift client to Cartfile Source: https://github.com/algolia/algoliasearch-client-swift/blob/main/README.md Add the Algolia Swift client to your Cartfile. The version requirement is ~> 9.0.0. ```ruby github "algolia/algoliasearch-client-swift" ~> 9.0.0 ``` -------------------------------- ### Value vs Reference Types: Prefer struct, use class for reference semantics Source: https://github.com/algolia/algoliasearch-client-swift/blob/main/AGENTS.md Recommends using struct (value type) for data models and class only when reference semantics are needed, such as for a client object. ```swift // Prefer struct (value type) for data struct SearchParams { ... } // ✓ // Use class only when reference semantics needed class SearchClient { ... } // Client needs reference ``` -------------------------------- ### Codable: SearchParams struct with CodingKeys Source: https://github.com/algolia/algoliasearch-client-swift/blob/main/AGENTS.md Shows a Codable model for JSON serialization. The CodingKeys enum maps the property hitsPerPage to the same key, which is redundant but explicit. ```swift // Models use Codable for JSON serialization struct SearchParams: Codable { let query: String let hitsPerPage: Int? enum CodingKeys: String, CodingKey { case query case hitsPerPage = "hitsPerPage" } } ``` -------------------------------- ### Transport Architecture: Configuration and Transport Source: https://github.com/algolia/algoliasearch-client-swift/blob/main/AGENTS.md Defines the core Configuration struct and Transport class for the Algolia Swift client. The Configuration holds appID, apiKey, hosts, and timeouts, while Transport uses URLSession with retry logic to perform requests. ```swift // Sources/Core/ public struct Configuration { let appID: String let apiKey: String let hosts: [RetryableHost] let readTimeout: TimeInterval let writeTimeout: TimeInterval } // Transport uses URLSession with retry logic class Transport { func request(...) async throws -> T } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.