我在SwiftUI视图中收到此错误,请参见下面的代码;我还得到“无法将类型为“列表”的返回表达式转换为类型为“某些视图”的返回表达式。
struct RestaurantView: View {
@State private var isSshowingMapActionSheet = false
@State private var isShowingSafariView = false
let restaurant: Restaurant!
var currenstatus: String {
if restaurant.isOpen {
return "Open"
}
return "Closed"
}
var mapActionSheetButtons: [ActionSheet.Button] = [
.default("Open in Maps", action: {
self.openInMaps()
}),
.default("Open in Google Maps", action: {
self.openInGoogleMaps()
}),
.cancel(Text("Cancel"))
]
lazy var mapActionSheet = ActionSheet(title: Text(""), buttons: mapActionSheetButtons)
func openInMaps() {
let placemark = MKPlacemark(coordinate: restaurant.location.coordinate, addressDictionary: [CNPostalAddressStreetKey as String: restaurant.address!])
let mapItem = MKMapItem(placemark: placemark)
mapItem.name = self.restaurant.name
mapItem.phoneNumber = self.restaurant.phone
let launchOptions = [MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving]
mapItem.openInMaps(launchOptions: launchOptions)
}
func openInGoogleMaps() {
if let url = URL(string: "comgooglemapsurl://maps.google.com/?q=\(restaurant.name.URLEncoded)@\(restaurant.location.coordinate.latitude),\(restaurant.location.coordinate.longitude)") {
UIApplication.shared.open(url, options: [:])
}
}
var body: some View {
List {
ListRow("ClockGlyph", action: {
}, label: {
OpenHoursView(restaurant)
})
ListRow("PhoneGlyph", action: {
UIApplication.shared.op
}, label: {
Text(self.restaurant!.phone)
})
if (restaurant.homepage != nil) {
return ListRow("SafariGlyph", action: {
isShowingSafariView.toggle()
}, label: {
Text(restaurant.homepage)
}
}
ListRow("PhoneGlyph", action: {
UIApplication.shared.canOpenURL(self.restaurant.phoneURL)
}, label: {
Text(self.restaurant.phone)
})
if restaurant!.facebookURL != nil {
ListRow("FacebookGlyph", action: {
}, label: {
Text(self.restaurant.facebookURL!)
})
}
ListRow("PinGlyph", action: {
self..isSshowingMapActionSheet.toggle()
}, label: {
VStack {
Text(restaurant!.address!.replacingOccurrences(of: "\n", with: ", "))
Text("Visa vägbeskrivning")
.font(.subheadline)
.foregroundColor(.gray)
}
})
.actionSheet(isPresented: $isSshowingMapActionSheet, content: {
mapActionSheet
})
.sheet(item: $isShowingSafariView, content: {
SafariView(url: restaurant.homepageURL)
})
}
}
这是我的ListRow
课:
struct ListRow<Label> : View where Label : View {
let image: Image
var action:(() -> ())
let label: () -> Label
init(_ image: Image, action: @escaping () -> Void, @ViewBuilder label: @escaping () -> Label) {
self.image = image
self.action = action
self.label = label
}
var body: some View {
Button(action: self.action) {
HStack(spacing: 10) {
image.resizable().frame(width: 20, height: 20)
self.label().font(.subheadline)
}
}
}
}
答案 0 :(得分:1)
有一些错字会导致编译失败。
使用SwiftUI可能会得到不是很有帮助的Xcode错误,您应该尝试注释掉不同的行,直到它给您带来更多信息为止。借助新的beta Xcode 11.4,它会更好。
通常,尝试构建较小的视图,将它们更多地分解为单独的结构,以便更容易发现错别字,这也将有助于编译器。