【问题标题】:Swift nil coalescing operator with arraySwift nil 合并运算符与数组
【发布时间】:2018-10-24 14:58:39
【问题描述】:

我正在尝试创建一个简单的待办事项列表。 在介绍 Realm 或 coreData 之前,我想对其进行测试,看看是否一切顺利。

我知道我可能可以在一些 if 条件 下完成这项工作,但我希望能够使用 nil coalescing 运算符(我只是喜欢它的简单性),我不确定它为什么不起作用。

我让它在没有它的情况下工作,但真的很感兴趣它为什么会这样。

当我启动应用程序时,它只是显示“未添加类别”,即使在我将一些项目添加到数组并打印出来之后,列表仍然保持不变。

import UIKit

class CategoriesTableView: UITableViewController {

  var testData = [FauxData]()

  override func viewDidLoad() {

    super.viewDidLoad()
    tableView.reloadData()

  }

  // MARK: - Data Methods

  override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)

    let data = testData[indexPath.row].categoryTitle ?? "No Category Added"
    cell.textLabel?.text = data

    return cell
  }

  override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return testData.count
  }

  @IBAction func addItem(_ sender: UIBarButtonItem) {
  CreateNewItem(item: "test")
  tableView.reloadData()
  }

  func CreateNewItem(item: String) {
    let newItem = FauxData()
    newItem.categoryTitle = item
    testData.append(newItem)
    print(item)
  }

}

这是 FauxData 类:

class FauxData {
  var categoryTitle: String?
}

对不起,如果这太简单或重复,我无法找到合适的答案。

【问题讨论】:

  • 当您启动应用程序时,您应该在表格中看不到任何行。您需要点击“添加”按钮才能看到文本项。
  • 但是当数组中没有项目时,我想要文本“未添加类别”,一旦输入数据,该文本就会被数据替换。
  • 需要明确的是,您发布的代码将在有数据时显示正确的数据,而在没有数据时将不显示任何内容。因此,您需要的唯一更改是在没有数据时显示一行。这是正确的吗?
  • 正是这样:)

标签: arrays swift null coalescing


【解决方案1】:

不幸的是,索引空数组会崩溃而不是返回 nil,因此您不能使用 nil 合并运算符。相反,使用 .isEmpty 属性和 ?: 运算符来实现您的目标:

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)

    let data = testData.isEmpty ? "No Category Added" : testData[indexPath.row].categoryTitle
    cell.textLabel?.text = data

    return cell
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return testData.isEmpty ? 1 : testData.count
}

注意:当数组为空时,您必须从 tableView(_:numberOfRowsInSection:) 返回 1,以便调用 tableView(_:cellForRowAt:) 以返回您的默认消息。


如果您实现safe array indexing,则可以使用nil 合并运算符

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)

    let data = testData[safe: indexPath.row]?.categoryTitle ?? "No Category Added"
    cell.textLabel?.text = data

    return cell
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return testData.isEmpty ? 1 : testData.count
}

【讨论】:

  • 它不仅像一个魅力,它仍然是非常干净和漂亮的代码!谢谢! Tnx 也是第二种解决方案,我喜欢安全的数组索引,以前从未见过!它看起来如此简单和有趣。 p.s.我实际上无法入睡,试图思考为什么它不能正常工作,我早上 3.50 起床只是为了看到你在那个时候发布了答案:)
猜你喜欢
  • 2015-03-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-26
  • 1970-01-01
  • 1970-01-01
  • 2016-09-03
相关资源
最近更新 更多