NameManager

@objcMembers public final class NameManager: NSObject

Manager for handling variable and procedure names.

Any names added to this manager are done so without case sensitivity.

  • A list of all names that have been added to the manager

    Declaration

    Swift

    public var names: [String]
  • The number of names that have been used

    Declaration

    Swift

    public var count: Int
  • Listeners for events that occur on this instance

    Declaration

    Swift

    public var listeners = WeakSet<NameManagerListener>()
  • Adds a given name to the list of names. If the same lowercase version of the name already exists in the list, an error is thrown.

    Throws

    BlocklyError: Thrown when trying to add a name that already exists.

    Declaration

    Swift

    public func addName(_ name: String) throws
  • Renames a name within the list.

    If oldName does not exist, nothing happens and false is returned. If oldName exists, but newName is already in the list, the newName is applied to all members with oldName.

    Declaration

    Swift

    public func renameName(_ oldName: String, to newName: String) -> Bool

    Return Value

    true if oldName existed in the list and was renamed to newName. false otherwise.

  • Rename the display name of an existing name. If the display name does not exist in the list, nothing happens and false is returned.

    Here is an example of this behavior:

    nameManager.addName("Foo") // Adds "Foo" to the list with the key name
    nameManager.renameDisplayName("FOO") // Renames "Foo" to "FOO"
    nameManager.renameDisplayName("bar") // This does nothing since "bar" does not exist in the list
    

    Declaration

    Swift

    public func renameDisplayName(_ displayName: String) -> Bool

    Return Value

    true if the displayName existed in the list with different case sensitivity and was renamed to displayName. false otherwise.

  • Attempts to remove a name from the list.

    NOTE: Any instance within self.listeners may cancel this request by implementing nameManager(:, shouldRemoveName:) and returning false.

    Declaration

    Swift

    public func removeName(_ name: String) -> Bool

    Return Value

    true if the name was found and removed. false otherwise.

  • Clears the list of names.

    Declaration

    Swift

    public func clearNames()
  • Returns if a given name has already been added.

    Declaration

    Swift

    public func containsName(_ name: String) -> Bool

    Return Value

    true if a name’s has been added. false otherwise.

  • Returns whether two names are considered equal, according to the NameManager.

    Declaration

    Swift

    public func namesAreEqual(_ name1: String, _ name2: String) -> Bool

    Return Value

    true if they are equal, false otherwise.

  • Generates a unique name within the scope of NameManager, based on a given name.

    If the base name is already unique, its name is returned directly. If the base name is not already unique, this method will add a unique number to the end of the name or automatically increment the name’s number if its suffix is already a number.

    e.g.

    let manager = NameManager()
    manager.generateUniqueName("foo", addToList: true) // Returns "foo"
    manager.generateUniqueName("foo", addToList: true) // Returns "foo2"
    manager.generateUniqueName("foo", addToList: true) // Returns "foo3"
    manager.generateUniqueName("Bar10", addToList: true) // Returns "Bar10"
    manager.generateUniqueName("Bar10", addToList: true) // Returns "Bar11"
    manager.generateUniqueName("Bar10", addToList: true) // Returns "Bar12"
    

    Declaration

    Swift

    public func generateUniqueName(_ name: String, addToList: Bool) -> String

    Return Value

    A unique name.