Error: unrecognized selector sent to instance 0x6000027f1ea0' & Process variables after receive parameters and before render view

Viewed 108

I have two views here: RoomDetailView and FeaturedTabView

In the FeaturedTabView, there are two variables imageString and roomImages, the imageString will receive data from RoomDetailView as parameter and RoomImages is for storing an array which is split from imageString after the imageString is assigned with the string coming from RoomDetailView.

I have two questions here:

  1. My app keep crashing and return error: terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Room roomImages]: unrecognized selector sent to instance 0x6000027f1ea0'. However, I am pretty sure that my variable type is correct and is aligned with my coreData attribute type.

  2. May I ask is there anywhere I can first process the string, split to array ( try using init but I dont think that is the correct way to go ) and assign to another roomImages variable before the view get rendered.

     struct RoomDetailView: View {
         let room: Room;
    
         var body: some View {
             ZStack{
                 VStack(spacing: 0){
                     FeaturedTabView(imageString: room.roomImages ?? "room-1,")
                         .padding(.vertical, 20)
                     })
                 }
                 .background(Color.white.ignoresSafeArea(.all, edges: .all))
             }
             .ignoresSafeArea(.all, edges: .top)
         }
     }
    
     struct FeaturedTabView: View {
             let imageString: String
             let roomImages: [String]
    
             var body: some View {
                 TabView{
                     ForEach(imageString, id: \.self){
                         img in FeaturedItemView(img: img)
                             .padding(.top, 10)
                             .padding(.horizontal, 15)
                     }
                 }
                 .tabViewStyle(PageTabViewStyle(indexDisplayMode: .always))
             }
         init(imageString: String){
         self.imageString = imageString
         self.roomImages = self.imageString.components(separatedBy: ",")
     }
    

    }

Room Entity

1 Answers

Try this example code, using roomImages in the ForEach loop.

// for testing
struct ContentView: View {
    var body: some View {
        RoomDetailView(room: Room())
    }
}

// for testing
struct Room: Identifiable {
    let id = UUID()
    var roomImages: String? = "room-1, room-2"  // <-- for testing
}

struct RoomDetailView: View {
    let room: Room
    
    var body: some View {
        ZStack{
            VStack(spacing: 0) {
                FeaturedTabView(imageString: room.roomImages ?? "room-1,")
                    .padding(.vertical, 20)
            }
        }
        .background(Color.white.ignoresSafeArea(.all, edges: .all))
        .ignoresSafeArea(.all, edges: .top)
    }
}

struct FeaturedTabView: View {
    let imageString: String
    let roomImages: [String]
    
    var body: some View {
        TabView {
            ForEach(roomImages, id: \.self){ img in  // <-- here
                FeaturedItemView(img: img)
                    .padding(.top, 10)
                    .padding(.horizontal, 15)
            }
        }
        .tabViewStyle(PageTabViewStyle(indexDisplayMode: .always))
    }
    
    init(imageString: String) {
        self.imageString = imageString
        self.roomImages = self.imageString.components(separatedBy: ",")
    }
}
// for testing
struct FeaturedItemView: View {
    let img: String

    var body: some View {
        Text(img)
    }
}
Related