Let's say I have the following simple Swift class:
class Burrito { enum Ingredients { case beans, cheese, lettuce, beef, chicken, chorizo } private let meats : Set<Ingredients> = [.beef, .chicken, .chorizo] var fillings : Set<Ingredients> init (withIngredients: Set<Ingredients>) { fillings = withIngredients } func isVegetarian() -> Bool { if fillings.intersect(meats).count > 0 { return false } else { return true } } }
If I create several instances of Burrito in the application, does the meat defined in memory set once separately from the memory allocated for each instance? I would suggest that the compiler would optimize this way, but so far I could not find the answer.
EDIT
With a lot of help from the accepted answer and comments about the logic in the isVegetarian () method, here is a modified class. Thanks everyone!
class Burrito { enum Ingredients { case beans, cheese, lettuce, beef, chicken, chorizo } private static let meats : Set<Ingredients> = [.beef, .chicken, .chorizo] var fillings : Set<Ingredients> init (withIngredients: Set<Ingredients>) { fillings = withIngredients } func isVegetarian() -> Bool { return fillings.intersect(Burrito.meats).isEmpty }
source share