SwiftUI-将位置添加到数据模型

时间:2019-12-22 22:10:06

标签: mapkit swiftui

我目前有一个数据模型,该模型具有一系列场所,并且我正在尝试为每个场所添加位置信息。我在定义结构时遇到问题。这是数据示例。

venues: [
                Venue (
                    title: "Holiday",
                    venueImage: "defaultVenue",
                    venueDesc: "ipsum lorem",
                    venueArea: "World East",
                    coordinate: .init(latitude: -33.852222, longitude: 151.210556),
                    venueItems: [
                        venueItem (
                            id: 101,
                            title: "Potato Dumpling",
                            itemURL: "default.png",
                            productDescription: "Potato Dumpling with Mushroom Sauce",
                            productPrice: 4.50,
                            productType: "Food",
                            newStatus: false,
                            diningPlan: false,
                            kidFriendly: true,
                            vegetarian: false,
                            glutenFree: false,
                            featuredProduct: false,
                            containsAlcohol: false
                        )])]

这是结构

struct Venue: Identifiable {

let id = UUID()
let title : String
let venueImage: String
let venueDesc : String
let venueArea: String
let coordinate: CLLocationCoordinate2D
let venueItems: [venueItem]

init(title: String,
     venueImage: String,
     venueDesc: String,
     venueArea: String,
     coordinate: CLLocationCoordinate2D,
     venueItems: Array<Any>) {
    self.title = title
    self.venueDesc = venueDesc
    self.venueArea = venueArea
    **self.venueItems = [venueItem]**
    self.coordinate = coordinate

}


}

我得到的错误是:无法将类型'[venueItem] .Type'的值分配为类型'[venueItem]'

1 个答案:

答案 0 :(得分:0)

struct VenueItem: Identifiable {
    var id = UUID()
    var title: String
    var itemURL: String
    var productDescription: String
    var productPrice: Float
    var productType: String
    var newStatus: Bool
    var diningPlan: Bool
    var kidFriendly: Bool
    var vegetarian: Bool
    var glutenFree: Bool
    var featuredProduct: Bool
    var containsAlcohol: Bool
}

struct Venue: Identifiable {
    var id = UUID()
    var title: String
    var venueImage: String
    var venueDesc: String
    var venueArea: String
    var coordinate: CLLocationCoordinate2D
    var venueItems: [VenueItem]
}

let venues = [
    Venue(
        title: "Holiday",
        venueImage: "defaultVenue",
        venueDesc: "ipsum lorem",
        venueArea: "World East",
        coordinate: .init(latitude: -33.852222, longitude: 151.210556),
        venueItems: [
            VenueItem(
                title: "Potato Dumpling",
                itemURL: "default.png",
                productDescription: "Potato Dumpling with Mushroom Sauce",
                productPrice: 4.50,
                productType: "Food",
                newStatus: false,
                diningPlan: false,
                kidFriendly: true,
                vegetarian: false,
                glutenFree: false,
                featuredProduct: false,
                containsAlcohol: false
            )
        ]
    )
]