skip to Main Content

The line VStack shows error "Instance method ‘sheet(item:onDismiss:content:)’ requires that ‘Int’ conform to ‘Identifiable’" but i think i dont make any mistake there? did i?

It feel like error come from my sheet complaining my Int should conform to ‘Identifiable’ however is not Int is an ‘Identifiable’ since it’s primitive? how would i solve this problem?

    struct SheetView: View {

    @State var bindingItemDiscounts: [Double] =
    [ 0.99, 0.25, 0.31, 11.99, 19.99, 0.1, 0.5]
    @State var discountMoreThanTen: Int?
    var body: some View {
    
    VStack(alignment: .leading) { <- this line
        Button {
            discountMoreThanTen = bindingItemDiscounts.count
        } label: { Text("Check Discounts") }

    }.sheet(item: $discountMoreThanTen) { newValue in
        if discountMoreThanTen ?? 0 <= 10 {
            
            List(bindingItemDiscounts, id: .self) { discount in
                Text("Discount : (discount)")
            }
            
        }
    }
 }
 }. 

2

Answers


  1. Chosen as BEST ANSWER

    You are using the Binding<Item?> sheet. The Item is a type, so it must conform to Identifiable in order to be identified uniquely.

    Also, Int is not a primitive in Swift. Int is basically a Type just like your other custom data types, so you need to make it Identifiable manually.

    Add this extension in your file will fix this problem:

    extension Int: Identifiable {
      public var id: Self { self }
    }
    

    If UUID() is required, then use this instead:

    extension Int: Identifiable {
      public var id: String {
        return UUID().uuidString
      }
    }
    

    1. There is no such concept as Primitive in Swift. The "Primitives" you refer to are normal structs.
    2. Int does not conform to Identifiable.

    To fix your problem just extend Int to conform to Identifiable like so:

    extension Int: Identifiable {
        public var id: Int { return self }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search