Arrays can take any class. A common class to place into an array is a custom class for your model. For example , you might have a class to keep track of cookies

//Using arrays in a model class
class Cookie{
var name = “”
var price = 0.00
var rating = 0
init(){}
init(name:String,price:Double,rating:Int){
self.name = name
self.price = price
self.rating = rating
}
}

In your view controller, particualrly one that has a UITableView or UIPickerView, you would define an array to hold your all your cookies:

var cookie = [Cookie]()
cookie +=  [Cookie(name: “Chocolate Chip”, price: 0.25, rating: 10)]
cookie += [Cookie(name: “Oatmeal”, price: 0.20, rating: 5)]
cookie += [Cookie(name: “Peanut Butter”, price: 0.20, rating: 4)]
cookie += [Cookie(name: “White Chocolate Macadamia”, price: 0.50, rating: 10)]
cookie +=  [Cookie(name: “Butter Ganache”, price: 0.35, rating: 9)]
cookie +=  [Cookie(name: “Chcocolate Cream”, price: 0.22, rating: 7)]

Iterate this array :

   for i in 0 ..<cookie.count  {
print(cookie[i].name)
}

(Visited 3 times, 1 visits today)

Leave a Reply

Your email address will not be published. Required fields are marked *