CoreData Ambiguous reference to member 'id' Xcode 12
Asked Answered
D

2

13

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?

Displeasure answered 7/8, 2020 at 11:8 Comment(4)
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.6Magdamagdaia
but if I used Xcode 11 (the current stable version) then I would not be able to develop for iOS 14 (beta version)Displeasure
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.Magdamagdaia
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 4Displeasure
D
10

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)
Displeasure answered 7/8, 2020 at 12:20 Comment(3)
Have you found a better workaround for this issue? I'm having the same on my side (Xcode Version 12.2 beta 4 (12B5044c)).Norvil
I didn’t look more into it. I left the question unanswered hoping that someone would answer itDispleasure
In the meantime, we can use setPrimitiveValue(UUID().uuidString, forKey: "id"). It's less elegant but it works.Norvil
C
1

Faced the same issue and was able to resolve it using more "public" functionality and without force unwrapping:

var string: String {
    NSExpression(forKeyPath: self).keyPath
}

Use it like this:

.init(format: "%K = %@", (\Athlete.id).string, athleteID)
Castillo answered 9/10, 2022 at 11:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.