Realm - 如何在列表中查找字符串 [英] Realm - how to find a string in a List
本文介绍了Realm - 如何在列表中查找字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我的对象:
class Cat: Object {让玩具 = List()}
如何在toys数组中找到一个玩具并删除它?
if let foundToy = cat.toys.filter(???).first {尝试!领域.写{领域.删除(foundToy)}}
解决方案
如果您要保留一个不同 String 的数组(即,toys 数组不包含重复项),您只需删除找到的第一个 String:
>
if let toyIndex = cat.toys.firstIndex(of: toyNameToBeDeleted) {尝试!领域.写{cat.toys.remove(at: toyIndex)}}
如果您尝试删除所有 String 对象 == 到某个玩具名称,请改为执行此操作:
试试吧!领域.写{cat.toys = cat.toys.filter { $0 != toyNameToBeDeleted }}
Here's my object:
class Cat: Object {
let toys = List<String>()
}
How can I find a toy in the toys array and delete it?
if let foundToy = cat.toys.filter(???).first {
try! realm.write {
realm.delete(foundToy)
}
}
解决方案
If you are keeping an array of distinct String (i.e., the toys array doesn't contain duplicates), you can just delete the first String found:
if let toyIndex = cat.toys.firstIndex(of: toyNameToBeDeleted) {
try! realm.write {
cat.toys.remove(at: toyIndex)
}
}
If you are trying to delete all String objects == to a certain toy name, do this instead:
try! realm.write {
cat.toys = cat.toys.filter { $0 != toyNameToBeDeleted }
}
这篇关于Realm - 如何在列表中查找字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文