gpt4 book ai didi

ios - swift4 Realm 模型 - 覆盖描述(等效于 java toString())

转载 作者:行者123 更新时间:2023-11-29 00:07:50 28 4
gpt4 key购买 nike

我有一个问题。我正在尝试覆盖描述方法以根据需要打印引用值。我读到这等同于 Java toString() 方法(我是 swift 的新手)

问题是,它不起作用,我找不到任何解决方案。我完全按照示例中的方式进行了操作。我做错了什么?

这是我的代码:

import Foundation
import RealmSwift

@objcMembers class Patient: Object{

//toString()
override var description: String{
return "Patient: Ref nr: \(self.referenceNumber), First name: \(self.firstName), Surname: \(self.surname) Email: \(self.email)"
}

var patientId:Int{
return self.patientId
}
dynamic var referenceNumber: String = ""
dynamic var firstName:String = ""
dynamic var surname:String = ""
dynamic var email:String = ""

convenience init(referenceNumber:String, firstName:String, surname:String, email:String) {
self.init()
self.referenceNumber = referenceNumber
self.firstName = firstName
self.surname = surname
self.email = email
}

现在,每当我尝试打印时:(我发布了更多代码以防 smith 错误早于打印)

var patient: Results<Patient>!

override func viewDidLoad() {
super.viewDidLoad();

let realm = RealmService.shared.realm
let patient = realm.objects(Patient.self) //tocheck: can I get only one element from Realm and set to store only 1 element?

//HERE IT GOES WRONG
print(patient) //case 1
print(patient.description) //case 2
}

它在控制台中打印:(在两种情况下相同)

Results<Patient> <0x7fccaaf03b90> (
[0] Patient {
referenceNumber = 231321;
firstName = James;
surname = Rodriguez;
email = jr@lalalla.pl;
}
)

为什么?

最佳答案

因为您正在尝试打印一个数组。试试 patient[0].description(patient 不是最好的名字,也许是 patients?)。

关于ios - swift4 Realm 模型 - 覆盖描述(等效于 java toString()),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47513296/

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