我有一个名为'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:类型患者没有下标成员
答案 0 :(得分:1)
您是否对[pSample]
和pSample
感到困惑?你在for循环中做的只是更新你患者的一个实例。
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)]
}