Help with making this instance method swiftier

Here I made a Cookie struct. I’m trying to make an instance method on the Cookie stuct that will add a specific instance of a Cookie type to a cookie jar array. ( I want to add specific cookies like snicker doodle and chocolate chip to my cookie jar)

The problem is I can’t figure out how to make it so I can call the addToCookie Jar method on an instance of cookie( ex. chocolateChip.addToCookieJar() ). When I do that, it will add the DEFUALT Cookie and NOT the SPECIFIC instance- chocolateChip of type Cookie. My work around to this issue was making an instance method that takes an argument of cookie type (specificCookie) but then calling the method seems so redundant (ex chocolateChip.addToCookieJar2(specificCookie: chocolateChip) )

struct Cookie {
    var name = "Default Cookie"
    var containsNuts = false

//version 1 adds the default instance of the Cookie type
   static var cookieJarArray = [Cookie]()
    func addToCookieJar() {
        Cookie.cookieJarArray.append(Cookie())
    }

//version 2 adds the specific instance of the Cookie Type
    static var cookieJarArray2 = [Cookie]()
    func addToCookieJar2(specificCookie: Cookie) {
         Cookie.cookieJarArray2.append(specificCookie)
    }
}

var chocolateChip = Cookie()
chocolateChip.name = "Chocolate Chip Cookie"
chocolateChip.containsNuts = false

//version 1
chocolateChip.addToCookieJar()
print("\(Cookie.cookieJarArray)")

The above prints the default cookie in the array “[Cookie(name: “Default Cookie”, containsNuts: false)]”

//version 2
chocolateChip.addToCookieJar2(specificCookie: chocolateChip)
print("\(Cookie.cookieJarArray2)")

This one prints the specific cookie in the array “[Cookie(name: “Chocolate Chip Cookie”, containsNuts: false)]”

Is there a way to make an instance method that will add the specific cookie to the array without passing in a parameter?

I found the answer. I have to use “self”

 static var cookieJarArray = [Cookie]()
    func addToCookieJar() {
        Cookie.cookieJarArray.append(self)
    }

Why cookieJarArray is inside your cookie struct? It doesn’t sound good to me. I’d rather create another struct that wraps your array

Why not? What do you mean by “create another struct that wraps your array”?

Just think real objects. A cookie is just that and know nothing about a cookie jar. This coupling is unnecessary if not wrong. I suggest a cookie struct with just cookie props and then you have CookieJar struct with your cookieJarArray Inside and methods to handle it

Then how would you call the addToCookieJar? It seems like it would be more complex. I liked it on the Cookie struct because then the function can be easily called as an instance method on the instance of the cookie.

Let cookie1 = Cookie("chocolate cookie", false)
Let cookie2 = Cookie() // default one

Let myJar = CookieJar()

myJar.addCookie(cookie1)
myJar.addCookie(cookie2)

There is the “easy” way and there is the swift(right) way. Your solution looks nice today to you. What about it in the long run with project more and more complex?

OK yeah I see that. I’m confused why the book had us put a Vampire thrall array on the Vampire type. I was really just borrowing from that format. I’m confused why it would be appropriate for the Vampire situation and not for this one.

I don’t know that section. I bet the author had his reason for doing that.

Anyway It’s good practice avoid this kind of coupling. That’s Why I would go for 2 different struct instead of the solution with the embedded array