For AI agents: a documentation index is available at the root level at /llms.txt and /llms-full.txt. Append /llms.txt to any URL for a page-level index, or .md for the markdown version of any page.
Sign inTry it free
DocsGuidesSDKsIntegrationsAPI docsTutorialsFlagship blog
DocsGuidesSDKsIntegrationsAPI docsTutorialsFlagship blog
  • SDKs
    • SDK concepts
    • SDK features
    • Client-side SDKs
      • .NET SDK reference
      • Android SDK reference
      • C++ SDK reference
      • Electron SDK reference
      • Flutter SDK reference
      • iOS SDK reference
        • iOS SDK 8.x to 9.0 migration guide
        • iOS SDK 7.x to 8.0 migration guide for Swift
        • iOS SDK 7.x to 8.0 migration guide for Objective-C
        • iOS SDK 5.x to 6.0 migration guide for Swift
        • iOS SDK 5.x to 6.0 migration guide for Objective-C
      • JavaScript SDK reference
      • Node.js SDK reference
      • React SDK reference
      • Roku SDK reference
      • Vue SDK reference
    • Server-side SDKs
    • AI SDKs
    • Edge SDKs
    • OpenFeature providers
    • Observability SDKs
    • Relay Proxy
Sign inTry it free
LogoLogo
On this page
  • Overview
  • Understanding the new LDValue type
  • Understanding the changes to configuration
  • Understanding the changes to creating users
  • Changes to variation functions
  • Changes to variationDetail functions
  • Recording custom events
SDKsClient-side SDKsiOS SDK reference

iOS SDK 5.x to 6.0 migration guide for Swift

Was this page helpful?
Previous

iOS SDK 5.x to 6.0 migration guide for Objective-C

Next
Built with

Overview

This topic explains how to adapt Swift code that currently uses a 5.x version of the iOS client-side SDK to use version 6.0 or later.

Sections in this topic address different changes and updates between versions 5.x and 6.0 of the SDK.

Understanding the new LDValue type

The 6.0 release of the iOS client-side SDK introduces a new type that represents a value of any valid JSON type: LDValue. To learn more about the new type, read the generated documentation. The LDValue enum implements several ExpressibleBy protocols allowing constants to be created the same as other Swift types, as shown below.

6.0 syntax
1let nullValue: LDValue = nil
2let boolValue: LDValue = true
3let numericValue: LDValue = 5.5
4let stringValue: LDValue = "abc"
5let complexValue: LDValue = ["abc": 123, "def": [false, true]]

Understanding the changes to configuration

The LDConfig class only requires changes if the application specifies the privateUserAttributes property. The type of the property has changed from [String] to [UserAttribute].

Here is an example that highlights the changes:

1var config = LDConfig(mobileKey: "example-mobile-key")
2config.privateUserAttributes = ["name", "premium"]

Setting "custom" as a private attribute no longer sets all custom attributes private. Only the custom attribute with the key "custom" would be considered private by the SDK.

Understanding the changes to creating users

Similarly to LDConfig.privateUserAttributes, LDUser.privateAttributes has been updated to use UserAttribute. Additionally LDUser.custom now user the LDValue type rather than Any for specifying the value of custom attributes.

Here is an example that highlights the changes:

1let privateAttributes: [String] = ["name", "jobFunction"]
2let customAttributes: [String: Any] = ["jobFunction": ["doctor"]]
3let user = LDUser(key: "example-user-key", custom: customAttributes, privateAttributes: privateAttributes)

The Equatable instance for LDUser has also changed. It now compares all properties, rather than only the key property.

1let user1 = LDUser(key: "example-user-key", name: "Sandy Smith")
2let user2 = LDUser(key: "example-user-key", name: "Jesse Smith")
3// Results in true
4user1 == user2

Changes to variation functions

The iOS SDK now provides functions per-type for retrieving the variation for a given flag. To retrieve variation values that are complex (JSON arrays or objects), use the new jsonVariation function.

Here is an example:

1let boolValue: Bool = LDClient.get()!.variation(forKey: "boolFlag", defaultValue: false)
2let arrayValue: [Any] = LDClient.get()!.variation(forKey: "arrayFlag", defaultValue: ["abc", "def"] as [Any])

Changes to variationDetail functions

The iOS SDK now provides function per-type for retrieving the detailed variation result for a given flag. To retrieve variation values that are complex, such as JSON arrays or objects, use the new jsonVariation function. The reason information associated with a LDEvaluationDetail has also changed from [String: Any]? to [String: LDValue]?.

Here is an example:

1let boolValue: LDEvaluationDetail<Bool> = LDClient.get()!.variationDetail(forKey: "boolFlag", defaultValue: false)
2let arrayValue: LDEvaluationDetail<[Any]> = LDClient.get()!.variationDetail(forKey: "arrayFlag", defaultValue: ["abc", "def"] as [Any])
3let arrayReason: [String: Any]? = arrayValue.reason

Recording custom events

If your application uses track to record custom events, remove try statements. This function no longer throws an error. Additionally, the data parameter type has changed from Any? to LDValue?. If your application provides the data parameter, you may need to update it to provide an LDValue type.

Here is an example:

1do {
2 let customData: Any = ["abc": 123]
3 try LDClient.get()!.track(key: "key", data: customData)
4} catch let error as LDInvalidArgumentError {
5 // Do something with the error
6} catch {}