79641418

Date: 2025-05-28 02:26:15
Score: 1
Natty:
Report link

I've been asking myself about using @Observable (replacement macro for ObservableObject) outside of SwiftUI for some time, it seems it was considered in the initial proposals but not implemented, and the workaround solutions feel like... workarounds. I was excited to see that Observations got implemented in Swift 6.2:

https://github.com/swiftlang/swift-evolution/blob/main/proposals/0475-observed.md

So now, outside of a SwiftUI view, you could write:

@Observable
final class Person {
  var firstName: String
  var lastName: String
 
  var name: String { firstName + " " + lastName } 

  init(firstName: String, lastName: String) { 
    self.firstName = firstName
    self.lastName = lastName 
  }
}

var person = Person(firstName: "John", lastName: "Doe")

let personNameChanges = Observations { person.name }

for newName in personNameChanges {
    print("Hello, \(newName)"
}
Reasons:
  • Long answer (-0.5):
  • Has code block (-0.5):
  • User mentioned (1): @Observable
  • Low reputation (1):
Posted by: Jim M