Swift Core Data NSPredicate静态字符串问题

时间:2019-03-27 20:38:08

标签: ios swift core-data nspredicate

我想使谓词更灵活地进行更改。我在源代码中有很多这样的字符串:

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)

1 个答案:

答案 0 :(得分:2)

使用%K替换密钥路径

{{1}}