Guides and Sample Code


Playground Book Format Reference

On This Page

PlaygroundKeyValueStore Class

An object that represents a key-value data store for the playground book.


A PlaygroundKeyValueStore object provides access to the key-value store for a playground book. The key-value store is available only in Swift Playgrounds and is persistent only in playground books.

Class Properties


The current key-value store.


  • static let current: PlaygroundKeyValueStore

Return value

A reference to the key-value store for the current playground or playground book.



Accesses the value associated with the given key in the playground book store for reading and writing.


  • subscript(key: String) -> PlaygroundValue? { get set }


  • key: The key to find in the store.

Return value

The value associated with key if key is in the key-value store; otherwise, nil.


This key-based subscript returns the value for the given key if the key is found in the key-value store, or nil if the key is not found. The returned value is of type PlaygroundValue Enumeration.

The following example sets the value of the key “animal” in the playground key-value store to “Llama” and then sets theAnimal by getting the value of that key from the key-value store.

  1. import PlaygroundSupport
  2. PlaygroundKeyValueStore.current.keyValueStore["animal"] = .string("Llama")
  3. var theAnimal: String? = nil
  4. if let keyValue = PlaygroundKeyValueStore.current.keyValueStore["animal"],
  5.                   case .string(let animalType) = keyValue {
  6.    theAnimal = animalType
  7. }