gpt4 book ai didi

ios - 在 Swift 中调用类时出现错误

转载 作者:行者123 更新时间:2023-11-28 06:45:56 25 4
gpt4 key购买 nike

我是 Swift 的新手。我有一个简单的 swift 类

class ListItem: NSObject {
let itemName: String
var completed: Bool

init(itemName: String, completed: Bool = false)
{
self.itemName = itemName
self.completed = completed
}
}

当我在 TableViewController 中引用此类时,出现以下错误:“实例成员 itemName 不能用于类型 ListItem”。

我的 TableViewController 代码(cellForRowAtIndexPath 方法)如下所示。

    let tempCell = tableView.dequeueReusableCellWithIdentifier("triggerCell")! as UITableViewCell
let listItem = listItems[indexPath.row]

// Downcast from UILabel? to UILabel
let cell = tempCell.textLabel as UILabel!


cell.text = ListItem.itemName

if (ListItem.completed)
{
tempCell.accessoryType = UITableViewCellAccessoryType.Checkmark;
}
else
{
tempCell.accessoryType = UITableViewCellAccessoryType.None;
}

return tempCell
}

我可能犯了一个基本错误,但我似乎无法确定哪里出了问题。

最佳答案

您正在调用类而不是您的实例。试试这个:

cell.text = listItem.itemName

if (listItem.completed)
etc...

编辑:更新 listItems 的创建。

let listItems = [ListItem("Viruses"), ListItem("Changes in weather"), etc...]

关于ios - 在 Swift 中调用类时出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36368759/

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