Swift 无法转换类型为';列表<;决不';返回类型';一些观点';
这是导致标题中出现错误的代码,它出现在带有Swift 无法转换类型为';列表<;决不';返回类型';一些观点';,swift,swiftui,swiftui-list,Swift,Swiftui,Swiftui List,这是导致标题中出现错误的代码,它出现在带有var body:some View的行上 import SwiftUI import Contacts import MapKit extension Restaurant { func openInMaps() { let placemark = MKPlacemark(coordinate: location.coordinate, addressDictionary: [CNPostalAddressStreetK
var body:some View的行上
import SwiftUI
import Contacts
import MapKit
extension Restaurant {
func openInMaps() {
let placemark = MKPlacemark(coordinate: location.coordinate, addressDictionary: [CNPostalAddressStreetKey as String: address!])
let mapItem = MKMapItem(placemark: placemark)
mapItem.name = self.name
mapItem.phoneNumber = 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: [:])
}
}
}
struct RestaurantView: View {
@State private var isSshowingMapActionSheet = false
@State private var isShowingSafariView = false
let restaurant: Restaurant!
var currenstatus: String {
return restaurant.isOpen ? "Öppet" : "Stängt"
}
lazy var mapActionSheetButtons: [ActionSheet.Button] = [
.default(Text("Öppna i Kartor"), action: {
self.restaurant.openInMaps()
}),
.default(Text("Öppna i Google Maps"), action: {
self.restaurant.openInGoogleMaps()
}),
.cancel(Text("Avbryt"))
]
lazy var mapActionSheet = ActionSheet(title: Text(""), buttons: mapActionSheetButtons)
init(_ restaurant: Restaurant) {
self.restaurant = restaurant
}
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)
})
} // End of if
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)
})
} // End of List
} // End of body
} // End of RestaurantView
struct RestaurantView_Previews: PreviewProvider {
static var previews: some View {
RestaurantView(Restaurant.allRestaurants.first!)
}
}
这是我的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)
}
}
}
}
struct ListRow:View其中Label:View{
让图像:图像
变量作用:(()->())
let label:()->label
init(image:image,action:@escaping()->Void,@ViewBuilder标签:@escaping()->label){
self.image=image
行动
self.label=标签
}
var body:一些观点{
按钮(操作:self.action){
HStack(间距:10){
image.resizeable().frame(宽:20,高:20)
self.label().font(.subheadline)
}
}
}
}
提前谢谢 只需阅读代码即可。您的ListRow
期望Image
作为第一个参数init(u-Image:Image,…
),但您使用字符串创建它们,如
ListRow("ClockGlyph", action:
,所以尝试使用
ListRow(Image("ClockGlyph"), action:
更新:删除返回中的如果如下
if restaurant.homepage != nil {
ListRow(Image("SafariGlyph"), action: {
isShowingSafariView.toggle()
}, label: {
Text(restaurant.homepage)
})
} // End of if
感谢您注意到这一点,但它并没有修复错误。@AndersFrohm,有了更新,我在我这边测试了您提供的代码,没有错误。使用Xcode 11.2进行了测试。您提供了准确的代码快照吗?另外,在每个操作中:
您应该通过self
引用局部变量,例如self.isShowingSafariView.toggle()
Ehh…试图检查您的代码。但是餐厅
,OpenHoursView
是什么?可能有什么地方出错了吗?请在提问时提供答案,以便人们可以为您提供方便的答案。不要在下面的评论部分进行无用的对话。