-
Notifications
You must be signed in to change notification settings - Fork 16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ObservationView不修改body的改进尝试 #1
Comments
Day2发现只要View中有StateObject就报错: The only dynamic property a 一种取巧的尝试@Observable final class Person {
var name: String
var age: Int
init(name: String, age: Int) {
self.name = name
self.age = age
}
}
class Ref: ObservableObject {
let randomColor = Color(
red: .random(in: 0 ... 1),
green: .random(in: 0 ... 1),
blue: .random(in: 0 ... 1)
)
}
struct ContentView: View {
private var person = Person(name: "Tom", age: 12)
@StateObject private var ref = Ref()
@State private var randomColor = Color(
red: .random(in: 0 ... 1),
green: .random(in: 0 ... 1),
blue: .random(in: 0 ... 1)
)
var body: some View {
VStack {
Text(person.name)
Text("\(person.age)")
HStack {
Button("+") { person.age += 1 }
Button("-") { person.age -= 1 }
Button("name") { person.name += "@" }
}
}
.padding()
.background(randomColor)
.foregroundColor(ref.randomColor)
}
}
extension ContentView {
private struct Impl: View {
private var person = Person(name: "Tom", age: 12)
@StateObject private var ref = Ref()
@State private var randomColor = Color(
red: .random(in: 0 ... 1),
green: .random(in: 0 ... 1),
blue: .random(in: 0 ... 1)
)
var body: some View {
ObservationView {
VStack {
Text(person.name)
Text("\(person.age)")
HStack {
Button("+") { person.age += 1 }
Button("-") { person.age -= 1 }
Button("name") { person.name += "@" }
}
}
.padding()
.background(randomColor)
.foregroundColor(ref.randomColor)
}
}
}
@ViewBuilder
@MainActor
private var observationBody: Impl {
var mutatingSelf = self
let ptr = withUnsafePointer(to: &mutatingSelf) { UnsafeRawPointer($0) }
ptr.load(as: Impl.self)
}
static func _makeView(view: SwiftUI._GraphValue<Self>, inputs: SwiftUI._ViewInputs)
-> SwiftUI._ViewOutputs {
Impl._makeView(view: view[\.observationBody], inputs: inputs)
}
static func _makeViewList(view: SwiftUI._GraphValue<Self>, inputs: SwiftUI._ViewListInputs)
-> SwiftUI._ViewListOutputs {
Impl._makeViewList(view: view[\.observationBody], inputs: inputs)
}
static func _viewListCount(inputs: SwiftUI._ViewListCountInputs) -> Int? {
Impl._viewListCount(inputs: inputs)
}
} 但失去了原body断点的能力(实际调用了Impl的body),增加了一点包体积。😂😂😂 |
进展咋样了 老哥 |
|
我也在尝试ObservationView这种形式 |
老哥 集成到项目里时 我遇到了一个问题:怎么把package结构移除掉呢 |
Macro的包官方只支持用SPM集成,貌似有Cocoapods的方式但是没有研究。 |
Cocoapods的形式,我试了一下 没有成功,因为没法解决宏替换的问题。
最近在排期内,闲了再来进行这个继续研究。 |
很好很强大,我 fork 下来发现无法在 @observable 修饰的对象中新增计算属性,比如在 Person 中添加 var myName: String { name } 会报错: 'accessor' macro cannot be attached to getter |
已同步apple swift lib 最新版本 |
通过_makeView可以不修改body的实现达成
inspired
包装一个协议
包装一个Macro
The text was updated successfully, but these errors were encountered: