gpt4 book ai didi

swift - 快速链接 Realm 中的对象的问题

转载 作者:可可西里 更新时间:2023-11-01 02:17:04 24 4
gpt4 key购买 nike

我在链接 Realm 中的对象时遇到问题。我阅读了标题为“Realm 中的嵌套对象”的问题和答案,并尝试了建议的答案,但它对我不起作用。

这是我的对象类:

import Foundation
import RealmSwift

class Job: Object {
dynamic var id = NSUUID().UUIDString
dynamic var name = ""
dynamic var address = ""
dynamic var phone = ""
dynamic var email = ""
dynamic var notes = ""
dynamic var material = 0.0
dynamic var edge = 0.0
dynamic var splash = 0.0
dynamic var discount = 0.0
dynamic var trip = 0.0
let rooms = List<Room>()

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

import Foundation
import RealmSwift

class Room: Object {
dynamic var id = NSUUID().UUIDString
dynamic var name = ""
dynamic var material = 0.0
dynamic var edge = 0.0
dynamic var splash = 0.0
dynamic var sinkType = ""
dynamic var sinkModel = ""
dynamic var numberOfSinks = 0
dynamic var faucet = ""
dynamic var rangeType = ""
dynamic var notes = ""
dynamic var jobs: Job?
let countertops = List<Countertop>()
//var linksToJob: [Job] {
// return linkingObjects(Job.self, forProperty: "rooms")
//}

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

}

import Foundation
import RealmSwift

class Countertop: Object {
dynamic var id = NSUUID().UUIDString
dynamic var depth = 0.0
dynamic var width = 0.0
dynamic var cabDescription = ""
dynamic var sqFt = 0.0
dynamic var room: Room?
//var linkToRoom: [Room] {
// return linkingObjects(Room.self, forProperty: "countertops")
//}
override static func primaryKey() -> String {
return "id"
}
}

以下是我用来保存数据的函数:

 @IBAction func saveButton() {
jobs.name = nameTF.text!
jobs.address = addressTF.text!
jobs.phone = phoneTF.text!
jobs.email = emailTF.text!
jobs.notes = notesTV.text!
jobs.discount = Double(discountTF.text!)!
jobs.material = Double(materialTF.text!)!
jobs.edge = Double(edgeTF.text!)!
jobs.splash = Double(splashTF.text!)!
jobs.trip = Double(tripTF.text!)!
do {
try! realm.write {
realm.add(jobs)
}
}

print(jobs)
// print(Realm.Configuration.defaultConfiguration.path!)

}
func saveData(){
rooms.name = nameTF.text!
rooms.material = Double(materialTF.text!)!
rooms.edge = Double(edgeTF.text!)!
rooms.splash = Double(splashTF.text!)!
rooms.notes = notesTV.text
rooms.sinkType = sinkTypeTF.text!
rooms.sinkModel = sinkModelTF.text!
rooms.numberOfSinks = Int(numberSinksTF.text!)!
rooms.faucet = faucetTF.text!
rooms.rangeType = rangeTF.text!
rooms.jobs?.id = keyValueLabel.text!
//rooms.linksToJob

do {
try! realm.write {
realm.add(rooms)
}
}

print(rooms)
}

和:

    @IBAction func addNextButton(sender: AnyObject) {

// self.realm.beginWrite()
let realm = try! Realm()


if widthTF.text != "" {
Calculations.depth = Double(depthTF.text!)!
Calculations.width = Double(widthTF.text!)!

let depthAdded = depthTF.text
cabinetDepth.append(Double(depthAdded!)!)


let widthAdded = widthTF.text
cabinetWidth.append(Double(widthAdded!)!)


let descriptionAdded = pickerLabel.text
cabinetDescription.append(descriptionAdded!)

let runningTotal = Calculations.squareFeet()
squareFeetToBeAdded.append(runningTotal)

let sum = squareFeetToBeAdded.reduce(0,combine: {$0 + $1})

let roundedSqFt = Double(round(sum * 10) / 10)

sqFtLabel.text = "\(roundedSqFt)"

countertops.depth = Double(depthTF.text!)!
countertops.width = Double(widthTF.text!)!
countertops.cabDescription = pickerLabel.text!
countertops.sqFt = Double(sqFtLabel.text!)!
//countertops.linkToRoom

do {
try realm.write {
realm.add(countertops)
}
print(countertops)
} catch {
print("an error occurred")
}


widthTF.text = ""

numberTable.reloadData()
widthTF.becomeFirstResponder()

}
}

当我保存数据并打印结果时,我得到的是:

Job {
id = 00F95F55-54D8-426E-B483-C990A4171002;
name = Ken;
address = Address;
phone = phone;
email = email;
notes = Notes :;
material = 8;
edge = 0;
splash = 4;
discount = 1;
trip = 0;
rooms = RLMArray <0x7ffef9df29c0> (

);
}
Room {
id = 7D3F86B9-FCD7-4CB7-AD6E-9B8141A7390C;
name = Kitchen;
material = 9;
edge = 0;
splash = 4;
sinkType = Undermount Kitchen;
sinkModel = 50/50 Stainless Steel;
numberOfSinks = 1;
faucet = Single Hole;
rangeType = Free Standing Range;
notes = Notes:;
jobs = (null);
countertops = RLMArray <0x7ffef9df3720> (

);
}

Countertop {
id = 992B8BAE-392F-4513-85DC-CBA191D2AE08;
depth = 25.5;
width = 65;
cabDescription = Cabinet;
sqFt = 11.5;
room = (null);
}

如您所见,链接返回空值。请告诉我我在这里做错了什么。注意:我注释掉了模型中的 linkingObjects,因为它导致了崩溃。我不确定为什么。

最佳答案

添加它们时,您应该使用对现有 Realm 对象的引用。所以与其这样:

func saveData(){
rooms.name = nameTF.text!
rooms.material = Double(materialTF.text!)!
rooms.edge = Double(edgeTF.text!)!
rooms.splash = Double(splashTF.text!)!
rooms.notes = notesTV.text
rooms.sinkType = sinkTypeTF.text!
rooms.sinkModel = sinkModelTF.text!
rooms.numberOfSinks = Int(numberSinksTF.text!)!
rooms.faucet = faucetTF.text!
rooms.rangeType = rangeTF.text!
rooms.jobs?.id = keyValueLabel.text!
//rooms.linksToJob

do {
try! realm.write {
realm.add(rooms)
}
}

print(rooms)
}

你会使用这样的东西:

let realm = try! Realm()
let room = realm.create(Room.self)
room.name = nameTF.text!
room.material = Double(materialTF.text!)!
...
let job = realm.create(Job.self)
job.name = "My job name"
...
room.job = job

因为看起来您是在不同的屏幕上输入这些内容,所以您将添加一个房间,然后您想要添加一个工作:

if let existingJob = realm.objects(Job).filter("name == '\(jobNameToLookup)'").first {
room.job = existingJob
}

对于列表,只需执行 room.countertops.append(newCountetop),其中 newCountertop 是 realm.create 的结果

关于swift - 快速链接 Realm 中的对象的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36827106/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com