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?