Swift

更新时间:
复制 MD 格式

Installation

To install the SDK, see alibabacloud-sdk-swift/alinlp-20200629. You can install the SDK using CocoaPods, Carthage, or Swift Package Manager.

Configure access credentials using environment variables

  1. Note:

    1. An AccessKey for an Alibaba Cloud account grants full access to all APIs. This poses a high security threat. We strongly recommend that you create and use a Resource Access Management (RAM) user for API calls and daily O&M. You can log on to the Resource Access Management (RAM) console to create a RAM user.

    2. To prevent security threats from key leaks, do not hardcode your AccessKey ID and AccessKey secret into your code. Instead, configure them as environment variables.

  2. Configuration for Linux and macOS

    export NLP_AK_ENV=<access_key_id>
    export NLP_SK_ENV=<access_key_secret>

    Replace <access_key_id> with your AccessKey ID and <access_key_secret> with your AccessKey secret. For more information about obtaining an AccessKey ID and AccessKey secret, see Step 2: Obtain an AccessKey for your account.

  3. Windows configuration method

    1. Create the environment variables NLP_AK_ENV and NLP_SK_ENV. Set their values to your AccessKey ID and AccessKey secret.

    2. Restart Windows.

Example

The following code shows how to call the sentiment analysis algorithm.

import Foundation
import AlibabacloudAlinlp20200629
import AlibabacloudOpenApi

import XCTest
import Tea


open class TestNLPAutom {
    
    public static func createClient(_ accessKeyId: String?, _ accessKeySecret: String?) throws -> AlibabacloudAlinlp20200629.Client {
        let config: AlibabacloudOpenApi.Config = AlibabacloudOpenApi.Config([
            "accessKeyId": accessKeyId!,
            "accessKeySecret": accessKeySecret!
        ])
        config.endpoint = "alinlp.cn-hangzhou.aliyuncs.com"
        return try AlibabacloudAlinlp20200629.Client(config)
    }
    
    @available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
    public static func main(_ args: [String]?) async throws -> Void {
        let client: AlibabacloudAlinlp20200629.Client = try TestNLPAutom.createClient(ProcessInfo.processInfo.environment["NLP_AK_ENV"],ProcessInfo.processInfo.environment["NLP_SK_ENV"])
        let request : AlibabacloudAlinlp20200629.GetSaChGeneralRequest = AlibabacloudAlinlp20200629.GetSaChGeneralRequest([
            "ServiceCode": "alinlp",
            "Text": "The service is very good"
        ])
        
        do {
            let response : GetSaChGeneralResponse =  try await client.getSaChGeneral(request)
            print(response.toMap());
        }
        catch {
            if error is Tea.TeaError {
                var e: Tea.ReuqestError = error as! Tea.ReuqestError
                print(e.code)
                print(e.message)
            }
        }
    }
}

Note: GetSaChGeneral in the GetSaChGeneralRequest and GetSaChGeneralResponse classes is the action name of the algorithm. Replace it with the action name for your target algorithm and update the request parameters. You can find the action name in the API Reference. It is the example value for the Action parameter in the Request Parameters section.

Troubleshoot call exceptions

If an exception occurs during a call, see Troubleshoot call exceptions (error code summary). Find the description that matches your error in the table. The description provides the cause of the error and a solution.