AnyCloneableData

Type erased container to hold data which gets passed between components

Overview

The key benefit of the Cloneable platform is that components can interface with other components, without knowing anything about those components. AnyCloneableData is a standardized format to send data to the platform framework (as outputs) and to receive data from the platform framework (as inputs)

AnyCloneableData wraps any data types that conforms to the CloneableDataType protocol. We've implemented a number of data types as CloneableDataType, both primitives (such as string, number, etc) and complex types such as bounding boxes.

Learn more about the general data types

Supported Types

We have defined a key set of standard AnyCloneableData data types. In the future you will be able to create your own custom data types and use in the platform

Using AnyCloneableData

In order to use AnyCloneableData in your app and components you will need to import CloneableCore

import CloneableCore

Basic Usage

The following examples will demonstrate basic usage of AnyCloneableData

Create a CloneableDataType and wrap with AnyCloneableData

Instantiate any of the already supported Data Types and pass it to the initializer of AnyCloneableData

import CloneableCore

// CloneableString conforms to CloneableDataType
let exampleString = CloneableString("Hello world") 
// Wrap the CloneableString with AnyCloneableData
let anyCloneableData = AnyCloneableData(data: exampleString)

Get wrapped data type

You can retrieve the wrapped data type by calling helper functions on AnyCloneableData.

let exampleString = CloneableString("Hello world")
let type: Any.Type = exampleString.valueType()
if let unwrappedStringValue: CloneableDataType = type as? CloneableString {
    ...
}

Get String from AnyCloneableData

There are several ways to extract the raw underlying value from AnyCloneableData. The following examples will walk through some basic examples to do this.

Get Raw Value helper function

The getRawValue() function will call a protocol function within the CloneableDataType Protocol and will return the raw value that the CloneableDataType holds.

import CloneableCore

if let stringValue = anyCloneableData.getRawValue() as? String {
    // Use stringValue here
    print(stringValue)
}

Last updated