我想使谓词更灵活地进行更改。我在源代码中有很多这样的字符串:
let predicate = NSPredicate(format: "bodyPart.name = %@", name)
let filteredExercises = ExerciseEntity.mr_findAll(with: predicate)
但是如您所见,如果某天我将BodyPart名称更改为MuscleGroup,它将导致类似以下问题:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'keypath bodyPart.name not found in entity <NSSQLEntity ExerciseEntity id=5>'
我正在使用
之类的关键路径来寻找解决方案let predicate = NSPredicate(format: "%@ = %@", \BodyPart.name, name)
答案 0 :(得分:2)
使用%K替换密钥路径
{{1}}