Block

@objcMembers public final class Block : NSObject

Class that represents a single block.

Note

To create a block programmatically, use a BlockBuilder.
  • A tuple representing a tree of connected blocks where:

    • rootBlock is the root of the tree
    • allBlocks is a list of all connected blocks (including rootBlock).

    Declaration

    Swift

    public typealias BlockTree = (rootBlock: Block, allBlocks: [Block])
  • A unique identifier used to identify this block for its lifetime

    Declaration

    Swift

    public let uuid: String
  • The type name of this block

    Declaration

    Swift

    public let name: String
  • Flag indicating if input connectors should be drawn inside a block (true) or on the edge of the block (false)

    Declaration

    Swift

    public var inputsInline: Bool
  • The absolute position of the block, in the Workspace coordinate system

    Declaration

    Swift

    public var position: WorkspacePoint
  • Flag indicating if this is a shadow block (true) or not (`false)

    Declaration

    Swift

    public let shadow: Bool
  • The .OutputValue connection for this block

    Declaration

    Swift

    public let outputConnection: Connection?
  • Convenience property for accessing self.outputConnection?.targetBlock

    Declaration

    Swift

    public var outputBlock: Block?
  • The .NextStatement connection for this block

    Declaration

    Swift

    public let nextConnection: Connection?
  • Convenience property for accessing self.nextConnection?.targetBlock

    Declaration

    Swift

    public var nextBlock: Block?
  • Convenience property for accessing self.nextConnection?.shadowBlock

    Declaration

    Swift

    public var nextShadowBlock: Block?
  • The .PreviousStatement connection for this block

    Note

    A block may only have one non-nil self.outputConnection or self.previousConnection

    Declaration

    Swift

    public let previousConnection: Connection?
  • Convenience property for accessing self.previousConnection?.targetBlock

    Declaration

    Swift

    public var previousBlock: Block?
  • If an inferior connection exists, returns either self.outputConnection or self.previousConnection (only one may be non-nil).

    Declaration

    Swift

    public var inferiorConnection: Connection?
  • List of connections directly attached to this block

    Declaration

    Swift

    public fileprivate(set) var directConnections = [Connection]()
  • List of inputs attached to this block

    Declaration

    Swift

    public private(set) var inputs: [Input]
  • The color of the block

    Declaration

    Swift

    public let color: UIColor
  • An optional mutator for this block

    Declaration

    Swift

    public var mutator: Mutator?
  • Tooltip text of the block

    Declaration

    Swift

    public var tooltip: String
  • The comment text of the block

    Declaration

    Swift

    public var comment: String
  • A help URL to learn more info about this block

    Declaration

    Swift

    public var helpURL: String
  • Flag indicating if this block may be deleted

    Declaration

    Swift

    public var deletable: Bool
  • Flag indicating if this block may be moved by the user

    Declaration

    Swift

    public var movable: Bool
  • Flag indicating if this block is disabled, which means it will be excluded from code generation.

    Declaration

    Swift

    public var disabled: Bool
  • Flag indicating if this block may be dragged by the user

    Declaration

    Swift

    public var draggable: Bool
  • Flag indicating if this block can be edited. Updating this property automatically updates the editable property on all child fields.

    Declaration

    Swift

    public var editable: Bool
  • Flag indicating if this block is at the highest level in the workspace

    Declaration

    Swift

    public var topLevel: Bool
  • The style that should be applied to the block during rendering.

    Declaration

    Swift

    public var style: Style
  • Listeners for events that occur on this block.

    Declaration

    Swift

    public var listeners = WeakSet<BlockListener>()
  • The layout associated with this block.

    Declaration

    Swift

    public weak var layout: BlockLayout?
  • Returns a list of all connections directly or indirectly connected to this block.

    Declaration

    Swift

    public func allConnectionsForTree() -> [Connection]

    Return Value

    A list of all connections directly or indirectly connected to this block.

  • Follows the chain of next connections starting from this block and returns the last block in the chain.

    Declaration

    Swift

    public func lastBlockInChain() -> Block
  • Follows the chain of input value connections starting from this block, returning the last input value connection. For each block in the chain, if there is exactly one input value, it either follows the input to the next block or returns the input value connection if it’s the last block. Nil is returned if any block in the chain has no or multiple input values.

    Declaration

    Swift

    public func lastInputValueConnectionInChain() -> Connection?

    Return Value

    The last input connection in the chain, or nil if none could be found.

  • Follows all input and next connections starting from this block and returns all blocks connected to this block, including this block.

    Declaration

    Swift

    public func allBlocksForTree() -> [Block]

    Return Value

    A list of all blocks connected to this block, including this block.

  • Finds the first input with a given name.

    Declaration

    Swift

    public func firstInput(withName name: String) -> Input?

    Return Value

    The first input with that name or nil.

  • Finds the first field with a given name.

    Declaration

    Swift

    public func firstField(withName name: String) -> Field?

    Return Value

    The first field with that name or nil.

  • Copies this block and all of the blocks connected to it through its input or next connections.

    Throws

    BlocklyError: Thrown if copied blocks could not be connected to each other.

    Declaration

    Swift

    public func deepCopy() throws -> BlockTree

    Return Value

    A BlockTree tuple of the copied block tree.

  • A convenience method that should be called inside the didSet { ... } block of instance properties.

    If property != oldValue, this method will automatically call notifyDidUpdateBlock(). If property == oldValue, nothing happens.

    Usage:

    var someString: String {
      didSet { didSetProperty(someString, oldValue) }
    }
    

    Declaration

    Swift

    public func didSetProperty<T: Equatable>(_ property: T, _ oldValue: T) -> Bool

    Return Value

    true if property is now different than oldValue. false otherwise.

  • Sends a notification to self.listeners that this block has been updated.

    Declaration

    Swift

    public func notifyDidUpdateBlock()
  • Append an input to the end of self.inputs.

    Declaration

    Swift

    public func appendInput(_ input: Input)
  • Insert an input at the specified position.

    Declaration

    Swift

    public func insertInput(_ input: Input, at index: Int)
  • Remove an input from the block. If the input doesn’t exist, nothing happens.

    Note

    The input must be disconnected from any connected shadow and non-shadow blocks prior to calling this method or else an error is thrown.

    Throws

    BlocklyError: Thrown if input has a shadow or non-shadow block still connected to it.

    Declaration

    Swift

    public func removeInput(_ input: Input) throws
  • Associates a mutator with this block and immediately calls mutateBlock() on it.

    Throws

    BlocklyError: Thrown if a mutator has already been associated with the block or if mutator.mutateBlock() throws an error.

    Declaration

    Swift

    public func setMutator(_ mutator: Mutator) throws
  • Specifies any styles that should be applied to the block during rendering.

    Declaration

    Swift

    @objcMembers open class Style: NSObject, NSCopying
  • Creates a new BlockBuilder from a JSON dictionary.

    Throws

    BlocklyError: Occurs if there is a problem parsing the JSON dictionary (eg. insufficient data, malformed data, or contradictory data). It also is thrown if a mutator (or extension) name has been defined in json, but no corresponding mapping could be found in mutators (or extensions).

    Declaration

    Swift

    public class func makeBuilder(
        json: [String: Any], mutators: [String: Mutator] = [:],
        extensions: [String: BlockExtension] = [:]) throws -> BlockBuilder

    Return Value

    A new block builder.

  • Creates a new tree of blocks from an XML object.

    Throws

    BlocklyError: Occurs if there is a problem parsing the xml (eg. insufficient data, malformed data, or contradictory data).

    Declaration

    Swift

    public class func blockTree(
        fromXMLString xmlString: String, factory: BlockFactory) throws -> BlockTree

    Return Value

    A BlockTree tuple of all blocks that were created.

  • Creates a new tree of blocks from an XML object.

    Throws

    BlocklyError: Occurs if there is a problem parsing the xml (eg. insufficient data, malformed data, or contradictory data).

    Declaration

    Swift

    public class func blockTree(fromXML xml: AEXMLElement, factory: BlockFactory) throws -> BlockTree

    Return Value

    A BlockTree tuple of all blocks that were created.

  • Returns an XML string representing the current state of this block and all of its descendants.

    Throws

    BlocklyError: Thrown if there was an error serializing this block or any of its descendants.

    Declaration

    Swift

    public func toXML() throws -> String

    Return Value

    The XML string.