【问题标题】:Cannot convert return expression of type 'List<Never, _>' to return type 'some View'无法将“List<Never, _>”类型的返回表达式转换为“某些视图”类型
【发布时间】:2020-02-25 12:07:24
【问题描述】:

这是导致标题错误的代码,它出现在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)
            }
        }
    }
}

提前致谢!

【问题讨论】:

  • Ehh... 试图检查您的代码。但是RestaurantOpenHoursView 是什么?那里的某个地方可能有错误吗?请在提问时提供Minimal, Reproducible Example,以便人们为您提供方便的答案。不要在 cmets 部分进行这样无用的对话,如下所示。

标签: swift swiftui swiftui-list


【解决方案1】:

只需阅读代码即可。你的ListRow 期望Image 作为第一个参数init(_ image: Image, ..,但是你用字符串创建它们,比如

ListRow("ClockGlyph", action:

,所以尝试使用

ListRow(Image("ClockGlyph"), action:

更新:如下删除if中的return

if restaurant.homepage != nil {
    ListRow(Image("SafariGlyph"), action: {
        isShowingSafariView.toggle()
    }, label:  {
        Text(restaurant.homepage)
    })
} // End of if

【讨论】:

  • 感谢您的注意,但它并不能修复错误。
  • @AndersFrohm,更新后我在我身边测试了你提供的代码,没有那个错误。使用 Xcode 11.2 测试。您是否提供了准确的代码快照?
  • 另外,在每个action: 中,您应该通过self 引用局部变量,例如self.isShowingSafariView.toggle()
猜你喜欢
  • 2021-05-09
  • 2020-07-10
  • 1970-01-01
  • 1970-01-01
  • 2021-01-18
  • 2017-07-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多