I’d like to use reflection to eliminate boilerplate in some SQLite code that I'm writing (for about the 10th time). I see Mirror, but it doesn't look like it does what I want.

Given a type, like this:

struct Foo {
    var id: UUID
    var name: String
    var startDate: Date?
    var amount: Double?
    func bar() { ... }
}

Is it it possible to:

  1. Iterate over those properties without an instance? (Mirror shows me property names if I have an instance.)
  2. See the declared types of the properties? (Mirror just gives me the names and run-time values, it seems.)
  3. See the functions like bar declared in the type?

1 and 2 would help me write something like createTable(for: Foo.self), because I could loop over the properties and decide what SQLite column styles to use.

3 Likes

AFAIK, not, but GitHub - wickwirew/Runtime: A Swift Runtime library for viewing type info, and the dynamic getting and setting of properties. may help

1 Like

Thanks, that library is interesting. I wonder if it depends on non-public binary layouts in a way that could cause apps using the library to break over time.

If my understanding is correct - only if there are breaking changes in Swift ABI. But then your app would need to recompiled.