6

I have a coredata entity with an attribute id of type String

when trying to reference that attribute from a key path it throws an error

let path = #keyPath(User.id) //Ambiguous reference to member 'id'

the codegen is set to Class Definition.

I tried to check the generated file for the class and I found that the entity class now confirms to Identifiable which requires id

I noticed that setting the deployment target to anything lower than iOS 13 will fix the issue (but I don't want to do that)

Xcode Version 12.0 beta 4 (12A8179i)

Is there a way to fix this without disabling codegen or changing the deployment target?

zombie
  • 4,242
  • 2
  • 19
  • 48
  • Try it on a stable version of Xcode, I have no problem doing the above on a similar entity class I have that conforms to Identifiable when the target is iOS 13 and Xcode is 11.6 – Joakim Danielson Aug 07 '20 at 11:44
  • but if I used Xcode 11 (the current stable version) then I would not be able to develop for iOS 14 (beta version) – zombie Aug 07 '20 at 11:46
  • I didn't tell you to switch but to test if it works on Xcode 11 because then you would know it is because of Xcode but if it doesn't work on Xcode 11 either then it is something with your code. Troubleshooting a problem to find the cause. – Joakim Danielson Aug 07 '20 at 11:51
  • yeah I totally understand I was just explain why I need it to work or an alternative on Xcode 12 by the way it works on Xcode beta 3 but not beta 4 – zombie Aug 07 '20 at 11:53

1 Answers1

4

Just a work around for this issue is to use the Objective c string presentation of the key path needed.

for example if you want to create a predicate for the id then instead of

let ambiguousPredicate = NSPredicate(format: "%K = %@", #keyPath(User.id), id) 

you can write

let workingPredicate = NSPredicate(format: "%K = %@", (\User.id)._kvcKeyPathString!, id)
zombie
  • 4,242
  • 2
  • 19
  • 48
  • Have you found a better workaround for this issue? I'm having the same on my side (Xcode Version 12.2 beta 4 (12B5044c)). – alpennec Nov 06 '20 at 13:52
  • I didn’t look more into it. I left the question unanswered hoping that someone would answer it – zombie Nov 06 '20 at 13:54
  • In the meantime, we can use setPrimitiveValue(UUID().uuidString, forKey: "id"). It's less elegant but it works. – alpennec Nov 06 '20 at 14:20