Guides and Sample Code

Developer

Playground Book Format Reference

PlaygroundValue Enumeration

An enumeration of the types that can be saved in the key-value store and can be exchanged in messages with always-on live views.

Enumeration Cases

array

A type used for encoding an array.

Declaration

  1. case array([PlaygroundValue])

Discussion

Encode an array of PlaygroundValue types for the key-value store or for live view messaging. The items in the array are other PlaygroundValue types.

The following example creates an array and adds it to the key-value store of a playground book. Line 3 creates an array with two playground value cases, a string and an integer. Line 4 constructs a playground value case of an array, and sets the associated value of that case to theArray. It then sets the "AnimalCounts" key in the playground book key-value store to the constructed array case.

  1. import PlaygroundSupport
  2. let theArray = [PlaygroundValue.string("llamas"), PlaygroundValue.integer(4)]
  3. PlaygroundKeyValueStore.current.keyValueStore["AnimalCounts"] = .array(theArray)

boolean

A type used for encoding a Boolean value.

Declaration

  1. case boolean(Bool)

data

A type used for encoding raw binary data.

Declaration

  1. case data(Data)

Discussion

Binary data can be used to represent objects initialized from custom classes.

date

A type used for encoding a date and time.

Declaration

  1. case date(Date)

dictionary

A type used for encoding a dictionary of key-value pairs.

Declaration

  1. case dictionary([String: PlaygroundValue])

Discussion

Encode a dictionary of PlaygroundValue values for the key-value store or for live view messaging. The values for keys are other PlaygroundValue types.

The following example creates a dictionary and adds it to the key-value store of a playground book. Lines 3 and 4 create a dictionary with two entries, a playground value case of a string for the "animal" key and a playground value case of an integer for the "count" key. Line 5 constructs a playground value case of a dictionary, and sets the associated value for the case to theDict. It then sets the "AnimalCountDict" key in the playground book key-value store to the constructed dictionary case.

  1. import PlaygroundSupport
  2. let theDict = ["animal": PlaygroundValue.string("Llama"),
  3.                "count": PlaygroundValue.integer(5)]
  4. PlaygroundKeyValueStore.current.keyValueStore["AnimalCountDict"] = .dictionary(theDict)

floatingPoint

A type used for encoding a floating point value.

Declaration

  1. case floatingPoint(Double)

integer

A type used for encoding an integer value.

Declaration

  1. case integer(Int)

string

A type used for encoding a string value.

Declaration

  1. case string(String)