[英]Realm Swift Filter Multiple ViewControllers
Re-asking the question, here, with some modifications to the code: 在这里重新询问问题,并对代码进行一些修改:
I have a class called Galaxies, each with a list of Planets 我有一个叫星系的课程,每个课程都有一个行星列表
class Galaxy: Object {
let planets = List<Planet>()
}
class Planet: Object {
dynamic var capitol = String()
dynamic var council = String()
dynamic var collective = String()
dynamic var signals = String()
}
I have 10 viewcontrollers, each representing a galaxy with various planets particular to that galaxy. 我有10个视图控制器,每个控制器代表一个星系,该星系具有该星系特有的各种行星。 How can I filter for particular planets for a particular galaxy?
如何过滤特定星系的特定行星?
When I instantiate a Results<Galaxy>!
当我实例化
Results<Galaxy>!
instance with an implicitly unwrapped optional, it compiles in the first Galaxy VC because I have starter data in my AppDelegate. 实例具有隐式展开的可选对象,它会在第一个Galaxy VC中进行编译,因为我的AppDelegate中包含启动程序数据。 When I do this
let galaxy = Results<Galaxy>? = nil
当我这样做时,
let galaxy = Results<Galaxy>? = nil
let galaxy = Results<Galaxy>? = nil
in the second Galaxy VC, the collection view goes blank. let galaxy = Results<Galaxy>? = nil
在第二个Galaxy VC中let galaxy = Results<Galaxy>? = nil
,集合视图变为空白。
This makes sense, because there is no data. 这是有道理的,因为没有数据。
So, I guess I have two questions: how do I instantiate a Results
object and filter for each view controller with no initial data? 因此,我想我有两个问题:如何为没有初始数据的每个视图控制器实例化一个
Results
对象并进行过滤?
I tried galaxies = realm.objects(Galaxy.self).filter("planets == %@", firstPlanet).sorted(byKeyPath: "capitol", ascending: false)
in viewDidLoad()
but this crashes: 我在
viewDidLoad()
尝试了galaxies = realm.objects(Galaxy.self).filter("planets == %@", firstPlanet).sorted(byKeyPath: "capitol", ascending: false)
,但这会崩溃:
libc++abi.dylib: terminating with uncaught exception of type NSException
Finally, var galaxy = Results<Galaxy>()
raises an exception cannot invoke initializer for type Results with no arguments
. 最后,
var galaxy = Results<Galaxy>()
引发一个异常cannot invoke initializer for type Results with no arguments
。
For Your View Controller A pass the planets from the selected Galaxy, prepare for segue method 对于您的View Controller A,从选定的银河中掠过行星,准备进行搜寻方法
var galaxies:[Galaxy] = realm.objects(Galaxy.self)
var selectedGalaxyIndex = 0
override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
if (segue.identifier == "segue_for_ViewController_B") {
// pass data to next view
let vc = segue.destination as viewControllerB
vc.planets = galaxies[selectedGalaxyIndex].planets
}
}
In Your View Controller B add 在您的View Controller B中添加
var planets:[Planet]?
If there's no initial data, a Realm query will be empty, which appears to be what you want. 如果没有初始数据,则Realm查询将为空,这似乎是您想要的。 For example:
例如:
class Galaxy: Object {
let planets = List<Planet>()
}
class Planet: Object {
dynamic var capitol = String()
dynamic var council = String()
dynamic var collective = String()
dynamic var signals = String()
}
let realm = try! Realm()
let galaxies = realm.objects(Galaxy.self)
print(galaxies.count) // => 0
As for the NSException
you're seeing, I suggest you read the whole message printed to the console or logs, as that should instruct you as to what you're doing wrong. 至于您看到的
NSException
,我建议您阅读打印到控制台或日志的整个消息,因为这应该指示您做错了什么。 From your code fragment, that might be that firstPlanet
is nil, meaning that planets == nil
is an illegal query, since a List
property can never be nil. 从您的代码片段中,可能是
firstPlanet
为nil,这意味着planets == nil
是非法查询,因为List
属性永远不会为nil。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.