我有一个SwiftUI视图,其中有一个@ObjectBinding progression: Progressable
,如下所示:
struct ProgressBarComponent : View {
@ObjectBinding var progression: Progressable
var progressAnimation: Animation {
Animation
.spring(stiffness: 43, initialVelocity: 3.2)
.speed(3)
.delay(0.07)
}
var body: some View {
VStack {
HStack {
Text(self.progression.name)
.font(.subheadline)
.color(.gray)
Spacer()
Text(self.progression.description)
.font(.subheadline)
.color(.gray)
}
.padding(.bottom, -8)
.padding(.horizontal, 4)
ZStack {
GeometryReader { geometry in
Capsule()
.fill(
Color(.displayP3,
red: 150 / 255,
green: 150 / 255,
blue: 150 / 255,
opacity: 0.1)
)
.overlay(
ZStack(alignment: .leading) {
Circle()
.fill(self.progression.gradient)
.position(x: 7, y: 7)
Capsule()
.size(
width: geometry.size.width * self.progression.progress,
height: geometry.size.height)
.fill(self.progression.gradient)
.animation(self.progressAnimation)
}
)
}
}
.frame(width: nil, height: 14, alignment: .leading)
}
.padding()
}
}
我创建的协议如下所示:
protocol Progressable: BindableObject {
var name: String { get }
var description: String { get }
var progress: CGFloat { get }
var gradient: LinearGradient { get set }
}
最后,我有一个实现BaseNutrient
协议的Progressable
类:
class BaseNutrient: Progressable {
let unitFormatter = NumberFormatter()
var didChange = PassthroughSubject<BaseNutrient, Never>()
var name: String
var description: String {
get {
return
"\(unitFormatter.string(from: NSNumber(value: Float(current)))!) / \(unitFormatter.string(from: NSNumber(value: Float(total)))!) \(unit.rawValue)"
}
}
var unit: NutrientUnit
var current: CGFloat
var total: CGFloat
var progress: CGFloat {
get {
return current / total
}
}
var gradient: LinearGradient
init(name: String, unit: NutrientUnit, total: CGFloat, colors: [Color] = [Color(red: 239.0 / 255, green: 120.0 / 255, blue: 221.0 / 255), Color(red: 239.0 / 255, green: 172.0 / 255, blue: 120.0 / 255)], start: CGFloat = 0) {
self.name = name
self.unit = unit
self.total = total
self.current = start
unitFormatter.locale = Locale.current
unitFormatter.numberStyle = .decimal
unitFormatter.maximumFractionDigits = 0
gradient = LinearGradient(gradient: .init(colors: colors), startPoint: .init(x: 0.0, y: 0),
endPoint: .init(x: 1.0, y: 0))
}
func addProgress(_ progress: CGFloat) {
current += progress
if current > total {
current = total
}
didChange.send(self)
}
func removeProgress(_ progress: CGFloat) {
current -= progress
if current < 0 {
current = 0
}
didChange.send(self)
}
}
如果我在SwiftUI视图中使用BaseNutrient
作为Progressable
的类型,则所有内容都会编译并运行良好。如果我改用协议Progressable
,则构建将永远不会结束并且会挂起,永远不会给我任何错误。有人知道会发生什么吗?
答案 0 :(得分:1)
当您将Progressable
用作直接类型时,编译无法推断BindableObject.PublisherType
关联类型。
添加<P: Progressable>
将有助于编译器。
不幸的是,编译器只是挂起,没有给出合理的错误消息。
请参阅:
struct ProgressBarComponent<P: Progressable> : View {
@ObjectBinding var progression: P
…