简体   繁体   English

计算领域对象列表中的项目数不起作用

[英]Count number of items in Realm Object List not working

I'm trying to count the number of exercises in a Realm List (contained within a "WorkoutSessionObject") to populate the right number of rows for a tableview but for some reason I can't work out it isn't letting me access the property? 我正在尝试计算领域列表(包含在“ WorkoutSessionObject”中)中的练习次数,以填充表格视图的正确行数,但是由于某些原因,我无法解决问题,这不是让我访问属性?

It's definitely retrieving the WorkoutSessionObject because I've tried printing it. 肯定是在检索WorkoutSessionObject,因为我尝试打印它。

Here's the code : 这是代码:

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

//filter for a specific workout
    let predicate = NSPredicate(format: "workoutID = %@", workoutID)
    let workoutExercises = realm.objects(WorkoutSessionObject.self).filter(predicate)

//access the 'exercises' list and count it - this isn't working?
    let numberOfExercises = workoutExercises.exercises.count
    return numberOfExercises
}

I've accessed the properties in a similar way for populating the cells but I'm obviously using index.row there. 我已经以类似的方式访问属性以填充单元格,但显然我在那儿使用了index.row。

The error I'm getting is 我得到的错误是

Value of type 'Results' has no member 'exercises' 类型“结果”的值没有成员“运动”

on the line that is marked as not working in the code 在代码中标记为无效的行上

Research there is an answer here Retrieve the List property count of realm for tableview Swift but that doesn't seem it would return the count in scope (it allows a print in the example but this isn't working in my code) 研究在这里有一个答案为Tableview Swift检索领域的List属性计数,但是似乎它不会返回范围内的计数(示例中允许打印,但这在我的代码中不起作用)

ie I've also tried this, which doesn't work because the count isn't accessible outside the scope : 即我也试过了,这是行不通的,因为无法在范围之外访问计数:

   func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        let predicate = NSPredicate(format: "workoutID = %@", workoutID)

//the below works to count but then not accessible outside scope to define number of rows :-(

        let workoutExercises = realm.objects(WorkoutSessionObject.self).filter(predicate)
        for exercises in workoutExercises {
            let exerciseCount = exercises.exercises.count
            return exerciseCount
        }
//return exercise count for table rows - this doesn't work because not in scope
        return exerciseCount
    }

Any ideas? 有任何想法吗?

The issue is that Realm's filter keeps the original type (just like Swift's built-in filter ), so the type of workoutExercises will actually be Results<WorkoutSessionObject> and not a single WorkoutSessionObject . 问题在于,Realm的filter保留原始类型(就像Swift的内置filter ),因此, workoutExercises的类型实际上是Results<WorkoutSessionObject>而不是单个WorkoutSessionObject

If you know that the workoutID property of the WorkoutSessionObject is always unique, you can simply call first on the Results instance. 如果您知道workoutID的财产WorkoutSessionObject始终是唯一的,你可以简单地调用firstResults实例。

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    let predicate = NSPredicate(format: "workoutID = %@", workoutID)
    let workoutExercises = realm.objects(WorkoutSessionObject.self).filter(predicate).first
    return workoutExercises?.exercises.count ?? 0
}

If you know that there will always be a matching WorkoutSessionObject , you can force unwrap workoutExercises . 如果您知道总会有一个匹配的WorkoutSessionObject ,则可以强制解开workoutExercises

If workoutID is actually a primaryKey, its even better to use let workoutExercises = realm.object(ofType: WorkoutSessionObject.self, forPrimaryKey: workoutID) instead of the filtered query. 如果workoutID实际上是primaryKey,则最好使用let workoutExercises = realm.object(ofType: WorkoutSessionObject.self, forPrimaryKey: workoutID)而不是过滤后的查询。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM