![](/img/trans.png)
[英]Error: Instance method 'onReceive(_:perform:)' requires that 'Int' conform to 'Publisher'
[英]VStack shows error "Instance method 'sheet(item:onDismiss:content:)' requires that 'Int' conform to 'Identifiable'"
VStack 行顯示錯誤“Instance method 'sheet(item:onDismiss:content:)' requires that 'Int'符合 'Identifiable'”但我認為我沒有犯任何錯誤? 我有嗎?
感覺好像錯誤來自我的工作表,抱怨我的 Int 應該符合“可識別”但是 Int 不是“可識別”,因為它是原始的? 我將如何解決這個問題?
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)")
}
}
}
}
}.
您正在使用Binding<Item?>
sheet 。 Item是一種類型,因此它必須符合Identifiable才能唯一標識。
此外, Int
不是 Swift 中的原語。 Int
基本上是Type就像您的其他自定義數據類型一樣,因此您需要手動使其可識別。
在您的文件中添加此擴展名將解決此問題:
extension Int: Identifiable {
public var id: Self { self }
}
如果需要 UUID(),請改用它:
extension Int: Identifiable {
public var id: String {
return UUID().uuidString
}
}
struct
。Int
不符合Identifiable
。 要解決您的問題,只需擴展Int
以符合Identifiable
如下所示:
extension Int: Identifiable {
public var id: Int { return self }
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.