๐ mismatching types
๐ AnyShape๏ผ่ฃก้ขๆ MyAnyShape
(custom type erasure) ็ๅฎ็พฉ
๐ has non-Sendable type๏ผ่ฃก้ขๆ MyShape
(custom enum) ็ๅฎ็พฉ
import SwiftUI
struct TempView: View {
@State var isOn = false
private let size: CGFloat = 20
var body: some View {
VStack {
text
Toggle("ๅๆ", isOn: $isOn)
.padding()
}
}
// โญ๏ธ ๆณจๆ๏ผ
// ้่ฃ็จไบไธๅๆนๆก๏ผไธๅ็ๅๅณๅๅฅ้ฝไธๅ(AnyShape, MyShape, MyAnyShape)๏ผ
// ๆไปฅ return type ๅฟ
้ ็จ `some Shape`๏ผ่ฎ compiler ่ช่กๅคๆทใ
private var dynamicShape: some Shape {
// โญ๏ธ ๆนๆกไธ๏ผไฝฟ็จ AnyShape (official type erasure)
// isOn ? AnyShape(circle) : AnyShape(roundedRect)
// โญ๏ธ ๆนๆกไบ๏ผไฝฟ็จ MyShape (custom enum)
// isOn ? MyShape.circle : MyShape.roundedRect(
// cornerSize: CGSize(width: size, height: size)
// )
// โญ๏ธ ๆนๆกไธ๏ผไฝฟ็จ MyAnyShape (custom type erasure)
isOn ? MyAnyShape(circle) : MyAnyShape(roundedRect)
}
private var text: some View {
Text("Clipped text in a circle")
.frame(width: 175, height: 100)
.foregroundColor(Color.white)
.background(Color.blue)
// ------------------------------------------------
// โญ๏ธ ่ฅไฝฟ็จๅๆ
ๅฝข็ๆ๏ผๅฟ
้ ๆณ่พฆๆณๆถ้คไธๅๅฝข็็ๅๅฅๅ้ก
.clipShape(dynamicShape)
// ๐ ไธ้ข็ๅฏซๆณๆ็ข็ "mismatching types" ็้ฏ่ชค
.clipShape(isOn ? Circle() : Capsule())
// ------------------------------------------------
.animation(.default, value: isOn)
}
private var roundedRect: some Shape {
RoundedRectangle(cornerSize:
CGSize(width: size, height: size)
)
}
private var circle: some Shape {
Circle()
}
}