Andrey
Andrey

Reputation: 2729

Type 'string' does not conform to protocol NilLiteralConvertible

In my code in Swift:

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

I am shown an error when running the build:

Type 'string' does not conform to protocol NilLiteralConvertible

This code always worked in Objective-C.

What could be wrong in Swift?

Upvotes: 10

Views: 3034

Answers (1)

Kirsteins
Kirsteins

Reputation: 27335

comment is declared as String and not String?. You cannot use nil there. Use "" instead.

 return NSLocalizedString(stringIdent, comment: "")

Upvotes: 9

Related Questions