如何访问从 Realm Swift 中的主键查询返回的对象的属性 [英] How to access properties of an object returned from primary key query in Realm Swift

查看:71
本文介绍了如何访问从 Realm Swift 中的主键查询返回的对象的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码:

 func jobSelected() {

    let businessName = realm.objects(UserSettings.self).last

    let results = realm.object(ofType: Job.self, forPrimaryKey: "uuid")

    print(results)

    businessNameLabel.text = "Bid From  \(businessName!.businessName)"
    jobNameLabel.text = "Job Name  \(results?.name)"
    jobAddressLabel.text = results?.address
    jobPhoneLabel.text = results?.phone
}

该函数返回正确的 uuid,但其他属性(results?.name、results?.address、results?.phone)返回 nil.这些属性用于在 tableview 单元格中填充标签.

The function returns the correct uuid, but the other properties (results?.name, results?.address, results?.phone) return nil. These properties are used to populate labels in a tableview cell.

感谢任何建议.

稍微更正:我实际上是在单击 tableview 单元格并进入另一个视图.然后我调用 jobSelected 函数,并尝试将获得的结果的值放入 UIView 中的常规标签中.我无法想象这很重要,因为我在 segue 中传递的只是来自单元格标签的实际 uuid 字符串.

A slight correction: I am actually clicking on a tableview cell and segueing into another view. Then I call the jobSelected function and I'm trying to put the values of the results obtained into regular labels in a UIView. I can't imagine that matters because all I am passing in the segue is the actual uuid string that comes from a cell label.

这是我的模型类的内容:进口基金会导入 RealmSwift

Here are the contents of my model class: import Foundation import RealmSwift

class Job: Object {

dynamic var name = ""
dynamic var address = ""
dynamic var phone = ""
dynamic var email = ""
dynamic var date = ""
dynamic var jobName = ""
dynamic var tripChg = 0.0
dynamic var notes = ""
dynamic var discount = 0.0
dynamic var uuid = NSUUID().uuidString
var rooms = List<Room>()

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


}

let room = LinkingObjects(fromType: Room.self, property: "job")
var thisJob: Room? {return room.first}

convenience init(uuid: NSUUID) {
    self.init()
    self.uuid = ""
}

}

推荐答案

已解决:

函数中出错的那一行是:

the line in the function that was wrong was:

let results = realm.object(ofType: Job.self, forPrimaryKey: "uuid)

应该是:

let results = realm.object(ofType: Job.self, forPrimaryKey: (segueLabel.text!))

segueLabel 保存uuid"属性的实际值.我正在学习……慢慢来.

The segueLabel holds the actual value of the "uuid" property. I'm learning... slowly.

感谢所有回复的人!

这篇关于如何访问从 Realm Swift 中的主键查询返回的对象的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆