I made a Cookie struct. On the struct I have a function, makeNewCookie(nameOfCookie:hasNuts:) so I can create instances of Cookies. Then I declare and initialize a Cookie variable, sugarCookie. I then call the makeNewCookie(nameOfCookie:hasNuts:) on sugarCookie to add properties to sugarCookie.
Is there a way to make it so I can declare sugarCookie and add it’s properties all on one line? Creating a cookie variable and then calling makeNewCookie(nameOfCookie:hasNuts:) on the next line seems redundant. If I’m going to be making a lot of cookies, I’d want a way to do it all on one line.
import Cocoa
struct Cookie {
var name = "Default Cookie"
var containsNuts = false
mutating func makeNewCookie(nameOfCookie: String, hasNuts: Bool){
self.name = nameOfCookie
self.containsNuts = false
}
}
var sugarCookie = Cookie()
sugarCookie.makeNewCookie(nameOfCookie: "Sugar Cookie", hasNuts: false)
print("\(sugarCookie)")