ios - 类型 'string' 不符合协议(protocol) NilLiteralConvertible

标签 ios swift

在我的 Swift 代码中:

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

运行构建时显示错误:

Type 'string' does not conform to protocol NilLiteralConvertible

此代码在 Objective-C 中始终有效。

Swift 有什么问题?

最佳答案

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

 return NSLocalizedString(stringIdent, comment: "")

关于ios - 类型 'string' 不符合协议(protocol) NilLiteralConvertible,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26074398/

相关文章:

ios - Apple 的 Swift 编程语言中的 Swift 之旅——interestingNumber 迭代 8 次

ios - 找不到 GStreamer Xcode 10.1 gst/gst.h

ios - 解析.com : Sending email to admin after new user signup

ios - UIWebView捏缩放以增加字体大小?

swift - 用 * 屏蔽名字和姓氏字符串

dictionary - 是否有可能有一个带有可变数组的字典作为 Swift 中的值

iOS:上传时如何检查FTP服务器上是否已经存在目录?

ios - 标签栏图标像素化

ios - 登录功能不工作..抛出错误...Firebase 和 Xcode 9

ios - SCNLookAtConstraint 在幕后做什么?