【问题标题】:Type 'string' does not conform to protocol NilLiteralConvertible类型“字符串”不符合协议 NilLiteralConvertible
【发布时间】:2014-09-27 11:55:55
【问题描述】:

在我的 Swift 代码中:

override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    let stringIdent = String(format: "section_1_%d", section)
    return NSLocalizedString(stringIdent, comment: nil)
}

运行构建时显示错误:

类型“字符串”不符合协议 NilLiteralConvertible

这段代码在 Objective-C 中一直有效。

Swift 有什么问题?

【问题讨论】:

    标签: ios swift


    【解决方案1】:

    comment 被声明为 String 而不是 String?。你不能在那里使用nil。请改用""

     return NSLocalizedString(stringIdent, comment: "")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-31
      • 1970-01-01
      • 1970-01-01
      • 2023-03-24
      相关资源
      最近更新 更多