Ios 快速传递密钥路径作为func的参数

Ios 快速传递密钥路径作为func的参数,ios,swift,nspredicate,swift-keypath,Ios,Swift,Nspredicate,Swift Keypath,我需要能够将#keypath传递给func以过滤我的CoreData记录 我是这样做的: func filteredExercises(with propertyKeyPath: #keyPath, filter: Any) { do { let filteredExercises = try CoreStore.fetchAll( From<ExerciseEntity>(),

我需要能够将#keypath传递给func以过滤我的CoreData记录

我是这样做的:

func filteredExercises(with propertyKeyPath: #keyPath, filter: Any) {

        do {
            let filteredExercises = try CoreStore.fetchAll(
                From<ExerciseEntity>(),
                Where<ExerciseEntity>("%K = %@", #keyPath(ExerciseEntity.muscle.name), filter)
            )

        } catch {

        }
    }
func filteredExercises(带属性ykeypath:#keyPath,filter:Any){
做{
让filteredExercises=尝试CoreStore.fetchAll(
From(),
其中(“%K=%@”,#键路径(ExerciseEntity.muscle.name),过滤器)
)
}抓住{
}
}

但可以肯定的是,keyPath不是一种类型,如何正确执行它?或者我需要准备一个过滤后的字符串,像谓词一样传递给func

如果您想使用
#keyPath
,那么它只是一个字符串。 (创建用于KVC的字符串的一种更安全的形式。)

将参数类型声明为
String
,您希望在其中接收
#keyPath
,并将其传递到任何接受
#keyPath
的位置

func filteredExercises(with propertyKeyPath: String, filter: Any) {

    do {
        let filteredExercises = try CoreStore.fetchAll(
            From<ExerciseEntity>(),
            Where<ExerciseEntity>("%K = %@", propertyKeyPath, filter)
        )

    } catch {

    }
}
func filteredExercises(带propertyKeyPath:String,filter:Any){
做{
让filteredExercises=尝试CoreStore.fetchAll(
From(),
其中(“%K=%@”,propertyKeyPath,筛选器)
)
}抓住{
}
}

如果您需要使用Swift native
KeyPath
,这是另一个问题。

如果您想使用
#KeyPath
,那么它只是一个字符串。 (创建用于KVC的字符串的一种更安全的形式。)

将参数类型声明为
String
,您希望在其中接收
#keyPath
,并将其传递到任何接受
#keyPath
的位置

func filteredExercises(with propertyKeyPath: String, filter: Any) {

    do {
        let filteredExercises = try CoreStore.fetchAll(
            From<ExerciseEntity>(),
            Where<ExerciseEntity>("%K = %@", propertyKeyPath, filter)
        )

    } catch {

    }
}
func filteredExercises(带propertyKeyPath:String,filter:Any){
做{
让filteredExercises=尝试CoreStore.fetchAll(
From(),
其中(“%K=%@”,propertyKeyPath,筛选器)
)
}抓住{
}
}
如果您需要使用Swift native
KeyPath
,这是另一个问题