Reputation: 421
I get this error in my SwiftUI View, see code below; I also get Cannot convert return expression of type 'List' to return type 'some View'.
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)
})
}
}
And here's my ListRow
class:
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)
}
}
}
}
Upvotes: 1
Views: 1648
Reputation: 1184
There a few typos that cause the compiling to fail.
With SwiftUI you can get Xcode errors that are not very helpful, you should try commenting out different lines until it gives you are more informative error. With the new beta Xcode 11.4 it should be better.
In general, try building smaller views, break them out into separate structs more so that it's easier to spot typos, it will also help the compiler.
Upvotes: 1