Use of unresolved identifier 'self

This is my code:

i

import SwiftUI

 struct RestaurantView: View {
    
    @State private var isSshowingMapActionSheet = false
    @State private var isShowingSafariView = false
    let restaurant: Restaurant!
    var currenstatus: String  {
        return restaurant.isOpen ? "Open" :  "Closed"
    }
    let mapActionSheetButtons: [ActionSheet.Button] = [
        .default(Text("Öppna i Kartor"), action: {
            self.restaurant.openInMaps()  <-- ERROR OCCURS HERE
        }),
        .default(Text("Öppna i Google Maps"), action: {
            self.restaurant.openInGoogleMaps()  <-- AND HERE
        }),
        .cancel(Text("Avbryt"))
    ]
    lazy var mapActionSheet = ActionSheet(title: Text(""), buttons: mapActionSheetButtons)
    
    init(_ restaurant: Restaurant) {
        self.restaurant = restaurant
    }
    
    
    
    var body: some View {
        List {
            ListRow(Image("ClockGlyph"), action: {
                
            }, label: {
                OpenHoursView(restaurant)
            })
            
            ListRow(Image("PhoneGlyph"), action: {
                UIApplication.shared.op
            }, label: {
                Text(self.restaurant!.phone)
            })
            
            if (restaurant.homepage != nil) {
                ListRow(Image("SafariGlyph"), action: {
                    self.isShowingSafariView.toggle()
                }, label:  {
                    Text(restaurant.homepage)
                })
            } // End of if
            
            ListRow(Image("PhoneGlyph"), action: {
                UIApplication.shared.canOpenURL(self.restaurant.phoneURL)
            }, label: {
                Text(self.restaurant.phone)
            })
            
            if restaurant!.facebookURL != nil {
                ListRow(Image("FacebookGlyph"), action: {
                }, label: {
                    Text(self.restaurant.facebookURL!)
                })
            }
            
            ListRow(Image("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!)
    }
 }

}

Could you tell where you get the error ?

In the code where it says ERROR OCCURS HERE.

So lines 13 and 16. Cannot testjust now. What if you skip self ?

You can reduce this problem to:

struct Test {
    let i: Int = 1
    let j: Int = self.i + 1
}

You can’t use

self
in the initial value for a property because it represents an initialisation ordering paradox. The property must be set up before
self
becomes valid, but the property can’t be set up because it depends on
self
being valid.

The standard ways around this are to write an explicit initialiser or to make this a

lazy var
. In this specific case I’m going to recommend the former.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"
Use of unresolved identifier 'self
 
 
Q