xcode Swift + Realm 新手:一个简单的 Realm 对象及其初始值设定项的问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36837623/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Swift + Realm newbie: Problems with a simple Realm object and its initializers
提问by Isaac
I've been a long time Objective-C developer and heard about Realm some weeks ago. On the other hand I've always wanted to migrate little by little to Swift, so I created a small project involving Realm + Swift.
我一直是 Objective-C 的开发人员,几周前听说过 Realm。另一方面,我一直想一点一点地迁移到 Swift,所以我创建了一个涉及 Realm + Swift 的小项目。
What does it mean? I'm a Swift + Realm newbie.
这是什么意思?我是 Swift + Realm 新手。
Anyway, I created a small demo/Proof of concept for a project I have in mind and I thought it had to be easier. But Xcode compiler says otherwise.
无论如何,我为我想到的一个项目创建了一个小演示/概念证明,我认为它必须更容易。但 Xcode 编译器另有说法。
My problem is with one of my Objects' initializer(s).
我的问题是我的对象的初始化程序之一。
My intentions were simple, but apparently Realm needs more initializers than I wanted.
我的意图很简单,但显然 Realm 需要比我想要的更多的初始化程序。
The code of one of my Realm Objects is this:
我的领域对象之一的代码是这样的:
import Foundation
import Realm
import RealmSwift
class Partida: Object
{
dynamic var id_partida: String
dynamic var inventario: Inventory?
required init ()
{
inventario = Inventory()
id_partida = "id_partida_test"
super.init()
}
required init(value: AnyObject, schema: RLMSchema) {
//fatalError("init(value:schema:) has not been implemented")
super.init(value: value, schema: schema)
}
required init(realm: RLMRealm, schema: RLMObjectSchema) {
//fatalError("init(realm:schema:) has not been implemented")
super.init(realm: realm, schema: schema)
}
override class func primaryKey() -> String? {
return "id_partida"
}
}
My original code only had the "normal" init initializer. But Xcode forced me to create two additional initializers more (value and realm ones).
我的原始代码只有“普通”初始化初始化程序。但是 Xcode 迫使我创建了两个额外的初始化程序(值和领域的)。
If I compile the code I've just pasted above, Xcode complains in the 2nd and 3rd required initializers, specifically in the super.init part. It says:
如果我编译刚刚粘贴在上面的代码,Xcode 会在第二个和第三个必需的初始化程序中抱怨,特别是在 super.init 部分。它说:
Property 'self.id_partida' not initialized at super.init call
I understand the meaning of it, but I don't know how to avoid the error because if I remove both super.init lines, the program crashes in runtime.
我明白它的意思,但我不知道如何避免错误,因为如果我删除了 super.init 两行,程序会在运行时崩溃。
if I uncomment the fatalError lines, they also crashes in runtime.
如果我取消对fatalError 行的注释,它们也会在运行时崩溃。
In fact I don't want to use these 2 initializers. If I could, I wouldn't add them, but Xcode needs to, apparently. The only code I really want to add to my object init function is "the simple" init function, which was the only part of code considered mine.
事实上,我不想使用这 2 个初始值设定项。如果可以,我不会添加它们,但 Xcode 显然需要添加。我真正想添加到我的对象 init 函数的唯一代码是“简单”的 init 函数,这是我认为的唯一代码部分。
I think I might have some concept misunderstandings in Realm + Swift + initializers.
我想我可能对 Realm + Swift + 初始化程序有一些概念误解。
I'm also having the feeling Xcode is forcing me to add code I don't need and/or I don't understand either.
我也有 Xcode 强迫我添加我不需要和/或我也不理解的代码的感觉。
Any help on understanding "required init" initializers in Realm will be more than welcome.
任何有关理解 Realm 中“必需的初始化”初始化程序的帮助都将非常受欢迎。
Official Realm + Swift documentation is beyond my knowledge as I don't understand many of its concepts even after re-reading them many times.
官方 Realm + Swift 文档超出了我的知识范围,因为即使重读了很多次,我也不理解其中的许多概念。
Google and StackOverflow haven't been really helpful this time...
谷歌和 StackOverflow 这次并没有真正有帮助......
Thanks.
谢谢。
采纳答案by TiM
Initializers in Swift definitely behave a bit differently to Objective-C, so I can definitely see the angle you're coming from here.
Swift 中的初始化程序的行为肯定与 Objective-C 略有不同,所以我绝对可以看到您从这里来的角度。
In this case though, since you're just using the initializer to set some default values, it's wholly un-necessary since you should be able to assign the default values to the properties themselves:
但是在这种情况下,由于您只是使用初始化程序来设置一些默认值,因此完全没有必要,因为您应该能够将默认值分配给属性本身:
class Partida: Object
{
dynamic var id_partida = "id_partida_test"
dynamic var inventario: Inventory? = Inventory()
override class func primaryKey() -> String? {
return "id_partida"
}
}
Let me know if that still doesn't work! :)
如果这仍然不起作用,请告诉我!:)
回答by Tj3n
Because it already has init ()
in Object
class, you are using subclass of Object, so you already have its init
in Realm object, you should give your var init value, like dynamic var id_partida: String = "id_partida_test"
, and then if you call let test = Partida()
it already has your 2 init value, other init should be marked with convenience
因为它已经init ()
在Object
类中,你使用的是Object的子类,所以你init
在Realm对象中已经有了它,你应该给你的var init值,比如dynamic var id_partida: String = "id_partida_test"
,然后如果你调用let test = Partida()
它已经有你的2 init值,其他init应该是标记为方便
When you save the Object to persistent store, it should be always have value, you can use Realm's optional then need read the documentation
当您将对象保存到持久存储时,它应该总是有价值的,您可以使用 Realm 的可选然后需要阅读文档
Here's my sample Realm class so that u got the idea:
这是我的示例 Realm 类,以便您了解:
import Foundation
import RealmSwift
import SwiftyJSON
class ProjectModel: Object {
dynamic var id: Int = 0
dynamic var name: String = ""
//Dont need this, call init() already have value
required init() {
id = 0
name = ""
super.init()
}
convenience init(fromJson json: JSON!){
self.init()
if json == nil {
return
}
id = json["id"].intValue
name = json["name"].stringValue
}
override class func primaryKey() -> String? {
return "id"
}
}