Uploaded image for project: 'Swift'
  1. Swift
  2. SR-5220

Expose API to retrieve string representation of KeyPath

    XMLWordPrintable

    Details

      Description

      Expose API to retrieve string representation of KeyPath

      We can build a type-safe NSPredicate using KeyPath if we have such an API. We can use `_kvcKeyPathString` currently, but it would be nice if it will be officially visible as a public API.

      For example, I created this experimental project using KeyPath and `_kvcKeyPathString`.

      Using this extension, you can type-safely write queries that were previously hard-coded with strings as shown below. In addition to type checking, a typo can be prevented, and code completion works.

      Before

      NSPredicate(format: "name == %@", "Katsumi")
      NSPredicate(format: "age > %@", 20)
      

      After

      Query(Person.self).filter(\Person.name == "Katsumi")
      Query(Person.self).filter(\Person.age > 20)
      
      Query(Person.self).filter(\Person.name > 20) // Compile error
      Query(Person.self).filter(\Dog.name == "John") // Compile error
      

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                Unassigned
                Reporter:
                kishikawakatsumi Kishikawa Katsumi
              • Votes:
                39 Vote for this issue
                Watchers:
                39 Start watching this issue

                Dates

                • Created:
                  Updated: