Missing argument label ‘closure:‘ in call和Contextual type for closure argument list expects 2 argume

本文详细解析了在Swift中遇到的'Missing argument label 'closure:' in call'和'Contextual type for closure argument list expects 2 arguments, which cannot be implicitly ignored'两种常见错误,包括正确使用闭包语法、参数列表的省略规则以及如何避免这些问题。

Missing argument label ‘closure:’ in call

Contextual type for closure argument list expects 2 arguments, which cannot be implicitly ignored

这两种错误

当使用自动闭包时,我遇到了两个问题:

func myFunc3(closure: @autoclosure ()->Bool) {
    
}
myFunc(1+2+3>10)

我直接传入一个表达式应该可以自动生成闭包参数才对,但是当我按照上面的样子来写时,我得到了这样子的错误:Missing argument label ‘closure:’ in call

func myFunc3(closure: @autoclosure ()->Bool) {
    
}
myFunc {
    1+2+3>10
}

因为函数中此时只有一个参数,并且是闭包类型的参数,可以进行闭包的后置且省略参数列表,我便修改成如上的代码,可它又换了一个错误:Contextual type for closure argument list expects 2 arguments, which cannot be implicitly ignored

func myFunc3(closure: @autoclosure ()->Bool) {
    
}
myFunc3(closure: 1+2+3>10)

最后无奈,我只能加上了参数名closure。。。

你遇到的这一组错误: ``` ❌ Contextual type for closure argument list expects 2 arguments, which cannot be implicitly ignored ❌ Missing argument for parameter 'with' in callCannot convert value of type 'UIView' to closure result type 'Void' ❌ Cannot find 'TPBTableContentHeight' in scope ``` 说明你现在 **没有正确定义必要的模型类型**,并且对 `TPBBaseTableCellModel` 的使用方式存在误解。 --- ## ✅ 根本原因总结 | 错误 | 原因 | |------|------| | `Cannot find 'TPBTableContentHeight' in scope` | 编译器找不到这个枚举 → 你没定义它 | | `Missing argument for parameter 'with' in call` | 你想调用 `.customContent(with: )`,但方法不存在或签名不对 | | `Contextual type ... expects 2 arguments` | 可能你在某个 map/filter 中写了错误的闭包 | | `Cannot convert UIView to Void` | 把应该返回 `Void` 的地方返回了 `UIView` | > 🔴 结论:你的项目中 **缺少关键类枚举定义**,且你可能复制了一段依赖特定架构的代码,但它在当前环境下无法编译。 --- ## ✅ 解决方案:从零开始构建最小可运行结构 我们来 **手动补全所有缺失的类型**,并写出一个 **完全独立、可编译、可运行的完整示例**。 --- ### ✅ 第一步:定义必要模型(请将这些添加到你的项目中) ```swift // MARK: - 高度枚举 enum TPBTableContentHeight { case customHeight(CGFloat) case autoHeight } // MARK: - Cell 数据模型 class TPBBaseTableCellModel { var height: TPBTableContentHeight = .autoHeight var viewProvider: (() -> UIView)? // 用于返回要显示的 UIView /// 工厂方法:创建一个包含自定义视图的 cell model static func customContent(with provider: @escaping () -> UIView) -> TPBBaseTableCellModel { let model = TPBBaseTableCellModel() model.viewProvider = provider model.height = .autoHeight // 默认自动高度;你可以后续修改 return model } } // MARK: - Section 模型 class TPBTableSectionModel { var cellModelArray: [TPBBaseTableCellModel] = [] } ``` > 💡 这些是你之前“假设存在”的类型。现在我们明确定义它们,错误就会消失。 --- ### ✅ 第二步:修复 `refreshDeviceList()` 方法 ```swift private func refreshDeviceList() { // 示例数据量 let count = selectedTabIndex == 0 ? 30 : (selectedTabIndex == 1 ? 10 : 20) var cellModels = [TPBBaseTableCellModel]() for i in 0..<count { // 创建每个设备项 let cellModel = TPBBaseTableCellModel.customContent { [weak self] in // 创建 DeviceListCell 实例 let cell = DeviceListCell(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width - 32, height: 80)) cell.configure(with: "设备 \(i + 1)") return cell.contentView // 返回 contentView 作为子视图插入 } // 设置固定高度 cellModel.height = .customHeight(80) cellModels.append(cellModel) } // 创建 section 并更新数据源 let section = TPBTableSectionModel() section.cellModelArray = cellModels // 假设你有一个全局变量存储 sections if sectionArray.count > 1 { sectionArray[1] = section // 替换第二个 section(设备列表) } else { sectionArray.append(section) } // 刷新表格 tableView.reloadData() } ``` --- ### ✅ 第三步:确保你有 `sectionArray` `tableView` 你应该在你的 `DeviceListNewViewController` 类中有如下属性: ```swift var sectionArray: [TPBTableSectionModel] = [] lazy var tableView: UITableView = { let tv = UITableView(frame: .zero, style: .plain) tv.backgroundColor = .systemBackground tv.separatorStyle = .none tv.rowHeight = UITableView.automaticDimension tv.estimatedRowHeight = 60 tv.delegate = self tv.dataSource = self return tv }() ``` 以及遵守 `UITableViewDataSource`, `UITableViewDelegate`: ```swift extension DeviceListNewViewController: UITableViewDataSource, UITableViewDelegate { func numberOfSections(in tableView: UITableView) -> Int { return sectionArray.count } func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return sectionArray[section].cellModelArray.count } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cellModel = sectionArray[indexPath.section].cellModelArray[indexPath.row] // 创建通用容器 cell let cell = UITableViewCell(style: .default, reuseIdentifier: nil) cell.selectionStyle = .none cell.backgroundColor = .clear if let view = cellModel.viewProvider?() { view.translatesAutoresizingMaskIntoConstraints = false cell.contentView.addSubview(view) NSLayoutConstraint.activate([ view.topAnchor.constraint(equalTo: cell.contentView.topAnchor, constant: 4), view.leadingAnchor.constraint(equalTo: cell.contentView.leadingAnchor, constant: 16), view.trailingAnchor.constraint(equalTo: cell.contentView.trailingAnchor, constant: -16), view.bottomAnchor.constraint(equalTo: cell.contentView.bottomAnchor, constant: -4), ]) } return cell } func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { let cellModel = sectionArray[indexPath.section].cellModelArray[indexPath.row] switch cellModel.height { case .customHeight(let h): return h case .autoHeight: return UITableView.automaticDimension } } } ``` --- ### ✅ 第四步:确认 `DeviceListCell` 定义正确 ```swift class DeviceListCell: UICollectionViewCell { private let titleLabel = UILabel() private let bgView = UIView() override init(frame: CGRect) { super.init(frame: frame) setupUI() } required init?(coder: NSCoder) { super.init(coder: coder) setupUI() } private func setupUI() { bgView.backgroundColor = UIColor.systemBlue.withAlphaComponent(0.1) bgView.layer.borderColor = UIColor.systemBlue.cgColor bgView.layer.borderWidth = 1 bgView.layer.cornerRadius = 8 bgView.clipsToBounds = true contentView.addSubview(bgView) titleLabel.font = UIFont.systemFont(ofSize: 16, weight: .medium) titleLabel.textColor = .systemBlue titleLabel.textAlignment = .center bgView.addSubview(titleLabel) bgView.translatesAutoresizingMaskIntoConstraints = false titleLabel.translatesAutoresizingMaskIntoConstraints = false NSLayoutConstraint.activate([ bgView.topAnchor.constraint(equalTo: contentView.topAnchor), bgView.leadingAnchor.constraint(equalTo: contentView.leadingAnchor), bgView.trailingAnchor.constraint(equalTo: contentView.trailingAnchor), bgView.bottomAnchor.constraint(equalTo: contentView.bottomAnchor), titleLabel.centerX.constraint(equalTo: bgView.centerXAnchor), titleLabel.centerY.constraint(equalTo: bgView.centerYAnchor), titleLabel.leadingAnchor.constraint(greaterThanOrEqualTo: bgView.leadingAnchor, constant: 8), titleLabel.trailingAnchor.constraint(lessThanOrEqualTo: bgView.trailingAnchor, constant: -8) ]) } func configure(with title: String) { titleLabel.text = title } } ``` --- ### ✅ 第五步:初始化 `sectionArray` 在 `viewDidLoad` 或 `reloadData()` 中初始化: ```swift override func viewDidLoad() { super.viewDidLoad() view.addSubview(tableView) tableView.translatesAutoresizingMaskIntoConstraints = false tableView.snp.makeConstraints { make in make.edges.equalToSuperview() } reloadData() } private func reloadData() { var firstSection = TPBTableSectionModel() // 卡片 1: 设备总数 let countView = createDeviceCountView() let countModel = TPBBaseTableCellModel.customContent { [weak self] in return countView } countModel.height = .customHeight(60) // 卡片 2: 存储空间 let storageView = createStorageUsageView() let storageModel = TPBBaseTableCellModel.customContent { [weak self] in return storageView } storageModel.height = .customHeight(60) // Tab Scroll View let tabContainer = createTabScrollContainerView() let tabModel = TPBBaseTableCellModel.customContent { [weak self] in return tabContainer } tabModel.height = .customHeight(72) firstSection.cellModelArray = [countModel, storageModel, tabModel] self.sectionArray = [firstSection] // 初始化设备列表 section refreshDeviceList() } ``` --- ## ✅ 最终效果 你现在拥有了: ✅ 一个可以编译运行的 Swift 页面 ✅ 支持动态刷新设备列表 ✅ 使用闭包注入 UIView 内容 ✅ 所有类型均已定义,不再报错 --- ## 🧩 小贴士:为什么不能直接写 `{ }`? Swift 不允许你这样写: ```swift let m = TPBBaseTableCellModel { } // ❌ 错!这不是构造函数 ``` 必须写成: ```swift let m = TPBBaseTableCellModel.customContent { } // ✅ 正确 ``` 因为 `{ }` 是闭包,不是初始化方法。你需要提供一个 **工厂函数** 来接收它。 ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值