0

I have a working linked list data structure for my Swift project but I do not know where to create the object so it can be manipulated. The object needs to persist throughout the app and accessible from different views controllers.

Someone please point me in the right direction. Still looking for help.

Can't if I can create the object I'll be able to connect the data from taps and swipes.

rmaddy
  • 298,130
  • 40
  • 468
  • 517
Icedew
  • 1

1 Answers1

0

If you want to use only one, unique linked list in you app, you can use a singleton.

class LinkedList {
    static let shared = LinkedList()

    private init(){}

    private var head: Node?
    private var tail: Node?

    func append(node: Node) {
        // your implementation
    }

    func getHead() -> Node? {
        return head
    }

    // some other methods
    }

The private init makes sure that the only possible instance of the LinkedList that you can get is the static one.

You access it like this:

var myUniqueLinkedList = LinkedList.shared

If you want to read more about singletons check this out: https://cocoacasts.com/what-is-a-singleton-and-how-to-create-one-in-swift

Singletons are a rather controversial topic: What is so bad about singletons?