如何查询List< Int>在Realm Swift上 [英] How to query List<Int> on Realm Swift

查看:131
本文介绍了如何查询List< Int>在Realm Swift上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何过滤出具有给定Int的RealmFilter.objectIds?

How can I filter out the RealmFilter.objectIds that has a given Int?

func delete(ids: [Int]) {
   let filterResultsToDelete = realm.objects(CRMRealmFilterResult.self).filter("ANY objectIds IN %@",ids)
//Crashes
}


class RealmFilterResult : Object {
    @objc dynamic var filterId: Int = 0
    let objectIds = List<Int>()

    override static func primaryKey() -> String {
        return "filterId"
    }
  }

推荐答案

这可能根本不是您想要的,但这是一个很好的练习.也许这会有所帮助.

This may not be at all what you want but it was a good exercise. Maybe this will help.

让我重申一下我想您要问的问题:您有一系列对象,每个对象都具有Int的List属性,并且您希望能够查询其对象中具有特定int的所有对象.列表

Let me re-state what I think you're asking: You've got a series of objects that each have a List property of Int's and you want to be able to query for all objects that have a particular int in their list

使用一个更真实的示例,假设我们有一个团队列表,并且我们在每个团队中保留了一个游戏得分列表(一个列表)

Using a more real-world example, suppose we have a list of teams and we keep a list of game scores (a list) within each team

class TeamObject: Object {
    @objc dynamic var object_id = NSUUID().uuidString

    let scoreList = List<ScoreObject>()

    override static func primaryKey() -> String? {
        return "object_id"
    }
}

我们有一个得分对象,该得分对象将得分存储为Int(以及其他详细信息,例如他们玩的人或日期)

and we have a score object that stores a score as an Int (and maybe other details like who they played or the date)

class ScoreObject: Object {
    @objc dynamic var score = 0
    let teamsWithScores = LinkingObjects(fromType: TeamObject.self, property: "scoreList")
}

为简单起见,让我们创建三个得分和两个团队,并在列表中给每个团队两个得分.

For simplicity, let's create three scores and two teams and give each team two scores in their list.

let score1 = ScoreObject()
score1.score = 1
let score2 = ScoreObject()
score2.score = 2
let score3 = ScoreObject()
score3.score = 3

let t1 = TeamObject()
t1.scoreList.append(score1)
t1.scoreList.append(score3)

let t2 = TeamObject()
t2.scoreList.append(score2)
t2.scoreList.append(score3)

并将其写入领域

try! realm.write {
    realm.add(t1)
    realm.add(t2)
}

从那里,我们可以获得得分为1的任何团队,这解决了获取具有包含给定int列表的对象的问题.

from there, we can get any team that has a score of 1, which solves the question of getting the objects that have a list that contain a given int.

let results = realm.objects(ScoreObject.self).filter("score IN %@", [1])
if results.count > 0 {
    for aScore in results {
        let teamsWithThisScore = aScore.teamsWithScores
        for team in teamsWithThisScore {
            print("score: \(aScore.score)")
            print("     id: \(team.object_id)")
        }
    }
} else {
    print("no teams with those scores")
}

您可以对此进行扩展,以获得具有多个得分(整数)的团队(对象)

you can expand on this to get teams (object) that have several scores (ints)

let results = realm.objects(ScoreObject.self).filter("score IN %@", [1,3])

正如我说的那样,它可能不是基础,但它确实以面向对象的方式提供了解决方案.

As I said, it may be off base but it does provide a solution in a more object oriented way.

这篇关于如何查询List&lt; Int&gt;在Realm Swift上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆