gpt4 book ai didi

ios - 类型 "Patient"没有下标成员

转载 作者:行者123 更新时间:2023-11-28 06:50:18 24 4
gpt4 key购买 nike

我有一个名为“Patient”的全局类和一个“Patient”类型的全局变量

class Patient{
var id: Int
var name: String
var mileage: Double

init(id:Int, name:String, mileage:Double){
self.id = id
self.name = name
self.mileage = mileage
}

var pSample: Patient?

这就是我将 pSample 分配给类的方式

for var i=0; i<nou; ++i{                   
pSample = Patient(id: json[0][i]["ID"].intValue, name: json[0][i]["Name"].stringValue, mileage: json[0][i]["Mileage"].doubleValue)
}

我打算在表格单元格中使用它

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("tableCell", forIndexPath: indexPath) as UITableViewCell

//everything refers to this
if pSample != nil {
let patient = pSample![indexPath.row] //error1
if let nameLabel = cell.viewWithTag(102) as? UILabel{
nameLabel.text = patient.name
}

if let scoreLabel = cell.viewWithTag(103) as? UILabel{
scoreLabel.text = String(patient.mileage)
}
}else{
let patient = patientSample[indexPath.row] as Patient
if let nameLabel = cell.viewWithTag(102) as? UILabel{
nameLabel.text = patient.name
}

if let scoreLabel = cell.viewWithTag(103) as? UILabel{
scoreLabel.text = String(patient.mileage)
}
}

为什么会出现此错误?

错误 1:类型 Patient 没有下标成员

最佳答案

您是否对 [pSample]pSample 感到困惑?您在 for 循环中所做的只是更新您拥有的一个 Patient 实例。

var pSample: [Patient] = []

for var i=0; i<nou; ++i{
pSample += [Patient(id: json[0][i]["ID"].intValue, name: json[0][i]["Name"].stringValue, mileage: json[0][i]["Mileage"].doubleValue)]
}

关于ios - 类型 "Patient"没有下标成员,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35098569/

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