我试图将数据保存在数据库中,但是有第一个表“ Actor”保存。
第二个桌子还没有保存,我不知道我会忘记的是班上的演员:
class Actor: Object, Codable {
@objc dynamic var name: String?
@objc dynamic var nickname: String?
@objc dynamic var image: String?
@objc dynamic var dateOfBirth: Int = 0
@objc dynamic var _powers: String = ""
var powers: [String]?
@objc dynamic var actorName: String?
var movies: [Movie]?
enum CodingKeys: String, CodingKey {
case name = "name"
case nickname = "nickname"
case image = "image"
case dateOfBirth = "dateOfBirth"
case powers = "powers"
case actorName = "actorName"
case movies = "movies"
}
convenience init(name: String, nickname: String, image: String, actorName: String, dateOfBirth: Int, powers: [String], movies: [Movie]) {
self.init()
self.name = name
self.nickname = nickname
self.image = image
self.powers = powers
self.actorName = actorName
self.movies = movies
self.dateOfBirth = dateOfBirth
self._powers = powers.joined(separator: ", ")
}
convenience required init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
let name = try container.decode(String.self, forKey: .name)
let nickname = try container.decode(String.self, forKey: .nickname)
let image = try container.decode(String.self, forKey: .image)
let actorName = try container.decode(String.self, forKey: .actorName)
let dateOfBirth = try container.decode(Int.self, forKey: .dateOfBirth)
let powers = try container.decode([String].self, forKey: .powers)
let movies = try container.decode([Movie].self, forKey: .movies)
self.init(name: name, nickname: nickname, image: image, actorName: actorName, dateOfBirth: dateOfBirth, powers: powers, movies: movies)
}
}
我的二等电影:
class Movie: Object, Codable {
@objc dynamic var name: String = "name"
@objc dynamic var year: Int = 0
enum CodingKeys: String, CodingKey {
case name = "name"
case year = "year"
}
convenience init(name: String, year: Int) {
self.init()
self.name = name
self.year = year
}
convenience required init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
let name = try container.decode(String.self, forKey: .name)
let year = try container.decode(Int.self, forKey: .year)
self.init(name: name, year: year)
}
}
以及负责备份的代码:
class ItemManager{
var delegate: UpdateDelegate?
func getDataFromURL(url: String){
guard let gitUrl = URL(string: url) else { return }
URLSession.shared.dataTask(with: gitUrl) { (data, response, error) in
guard let data = data else { return }
do {
let decoder = JSONDecoder()
let jsonData = try decoder.decode(Item.self, from: data)
self.delegate?.didUpdate(sender: self, actor: jsonData.actor)
DispatchQueue.main.async {
let realm = try! Realm()
try! realm.write {
realm.add(jsonData.actor)
realm.add(jsonData.actor.movies![0])
}
}
} catch let err {
print("Err", err)
}
}.resume()
}
如您所见,我正在使用可编码方法来检索json数据,而我正尝试备份该数据 演员对象保存成功,我不明白为什么电影对象不保存,我在做完全一样的事情
答案 0 :(得分:0)
对于领域对象数组有一个特殊的关键字。在您的情况下,请更换
var powers: [String]?
与
let powers: List<String> = List<String>()
不确定可选项。我没有找到有关数组可选性的任何信息。也许您需要将其包装到RealmOptional
,但是您真的需要一个可选的数组吗?电影也一样。