Environment

public class Environment : Collection, ExpressibleByDictionaryLiteral

A class that is used to capture and represent the os environment variables.

This class can be passed to TraceLog.configure(mode:writers:environment:) methods to configure it using the current Environment settings.

Environment is also like a Swift Dictionary so it can be used just like a dictionary including subscripting.

See also

TraceLog.configure(mode:writers:environment:) for usage.

See also

TraceLog.configure(writers:environment:) for usage.

Initialization

  • Creates a new Environment instance from a dictionary literal.

    The following example creates an Environment instance with the the elements of the dictionary literal:

    let environment: Environment = ["LOG_ALL": "ERROR", "LOG_TAG_MyTag": "TRACE1"]
    

    Declaration

    Swift

    public required init(dictionaryLiteral elements: (Key, Value)...)

    Parameters

    dictionaryLiteral

    A dictionary literal representing the environment variables that will be available to TraceLog.

  • Creates a new Environment instance with any collection type with an element type of [Environment.Key: Environment.Value].

    The following example creates an Environment instance with the contents of the dictionary type values:

    let values: [String : String] = ["LOG_ALL": "ERROR", "LOG_TAG_MyTag": "TRACE1"]
    
    let environment = Environment(values)
    

    Declaration

    Swift

    public init<T>(_ elements: T) where T : Collection, T.Element == Environment.Element

    Parameters

    elements

    Any Collection type whose Element is equal to [Environment.Key: Environment.Value].

  • Creates a new Environment instance and fills it with the variable set in the current OS environment.

    The following example creates an Environment instance with the current contents of the OS environment:

    let environment = Environment()
    

    Declaration

    Swift

    public init()

Indexing Elements

  • Returns the position immediately after the given index.

    Declaration

    Swift

    public func index(after index: Index) -> Index

    Parameters

    i

    A valid index of the collection. i must be less than endIndex.

    Return Value

    The index value immediately after i.

  • Always zero, which is the index of the first element when non-empty.

    Declaration

    Swift

    public var startIndex: Index { get }
  • A “past-the-end” element index; the successor of the last valid subscript argument.

    Declaration

    Swift

    public var endIndex: Index { get }

Accessing Elements

  • Accesses the element at the specified position.

    The following example accesses an element of an Environment through its subscript to print its value:

    let environment: Environment = ["LOG_ALL": "ERROR", "LOG_TAG_MyTag": "TRACE1"]
    print(environment[1])
    // Prints "TRACE1"
    

    Declaration

    Swift

    public subscript(position: Index) -> Element { get }

    Parameters

    position

    The position of the element to access. position must be a valid index of the Environment that is not equal to the endIndex property.

  • Set or get the element with the specified key.

    The following example accesses an element of an Environment through its subscript to get and print its value:

    let environment: Environment = ["LOG_ALL": "ERROR", "LOG_TAG_MyTag": "TRACE1"]
    
    print(environment["LOG_ALL"]) // Prints "ERROR"
    

    The following example accesses an element of an Environment through its subscript to set and print its value:

    environment["LOG_TAG_AnotherTag"] = "INFO"
    
    print(environment["LOG_TAG_AnotherTag"]) // Prints "INFO"
    

    Declaration

    Swift

    public subscript(key: Key) -> Value? { get set }

    Parameters

    key

    The key of the element to access.

Supporting Types

  • Key

    Declaration

    Swift

    public typealias Key = String
  • Declaration

    Swift

    public typealias Value = String
  • The element type of a Environment: a tuple containing an individual key-value pair.

    Declaration

    Swift

    public typealias Element = (key: Key, value: Value)
  • The index type of an Environment.

    Declaration

    Swift

    public typealias Index = DictionaryIndex<Key, Value>