【问题标题】:Filtering realm objects with Swift 3 using Function使用 Swift 3 使用 Function 过滤领域对象
【发布时间】:2017-08-10 10:11:33
【问题描述】:

我目前正在将我的应用从 Swift 2 升级到 Swift 3,并试图弄清楚谓词现在如何与 Realm 一起使用。

我有这个简单的代码:

let exercises = category.sections[indexPath.section].exercises.filter { (exercise) in
    self.isVisible(exercise)
}

let exercise = exercises[indexPath.row]

isVisible 函数在哪里:

func isVisible(_ exercise: RepositoryExercise) -> Bool {
    let firstSet = exercise.sets[0]

    if (exercise.sets.count > 1) {
        return true
    } else {
        if (firstSet.seconds > 0 || firstSet.reps > 0) {
            return true
        }
    }

    return exercise.visible
}

我的问题开始于 LazyFilterBidirectionalCollection 的使用。

现在在 Swift 3 中过滤集合的首选方法是什么?我正在考虑使用 NSPredicate,但重写此函数以使用它可能非常困难/看起来很复杂。

谢谢

【问题讨论】:

  • 您要过滤的category.sections[indexPath.section].exercises 的类型是什么?
  • List 这是一个领域对象。下面用我的solution回答。

标签: swift swift3 realm


【解决方案1】:

https://github.com/realm/realm-cocoa/issues/1046之后不久发现了这个话题

将我的列表包装在 Array() 中以便对其进行过滤。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-26
    • 1970-01-01
    相关资源
    最近更新 更多