在运行时添加更多对象。

时间:2014-07-30 17:51:39

标签: swift

我有一个餐馆的对象,其中包含姓名,评级和图片名称。你可以在下面看到。如何在我的餐馆列表中添加更多餐馆?

    class Restaurant {
    var name : String = ""
    var rating : Float = 0
    var image : UIImage?
    init(name:String, rating: Float, imageName:String){
        self.name = name
        self.rating = rating
        self.image = UIImage(named:imageName)
    }
}

var restaurants: [Restaurant] = [
    Restaurant(name:"mikes", rating:5, imageName:"0"),
    Restaurant(name:"mexican", rating:8, imageName:"1"),
    Restaurant(name:"french",rating:3, imageName:"2"),
    Restaurant(name:"hippie",rating:9, imageName:"3"),
    Restaurant(name:"Greek", rating:0, imageName:"mold")
]

1 个答案:

答案 0 :(得分:1)

您可以使用:

restaurants += Restaurant(name"Another", rating:10, imageName:"AwesomeImageName")