swift - 在 Swift 中,什么是 DictionaryLiteralConvertible?

标签 swift

如果将以下简单的代码片段放入 Playground 中:

import Foundation

let humanReadableNames = [
    "test": NSLocalizedString("foo")
]

你会得到编译器错误:

Cannot confer the expression's type 'Dictionary' to type 'DictionaryLiteralConvertible'

Swift 函数 NSLocalizedString定义为返回 String ,所以我不明白为什么这个变量的类型不只是 Dictionary<String, String> .

最佳答案

看来您需要将注释参数添加到 NSLocalizedString() 中。

let humanReadableNames = [
            "test": NSLocalizedString("foo", comment:"bar")
        ]

关于swift - 在 Swift 中,什么是 DictionaryLiteralConvertible?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24072305/

相关文章:

ios - 为什么 OAuth 重定向在 iOS 13 中不起作用,但在 iOS 12 中却起作用?

IOS Swift 读取 PCM Buffer

ios - 为什么 Swift 中的 postNotification 在第二次调用时失败?

ios - CAShapeLayer 位置不正确

ios - ViewController 的闪烁,其中 textField 是第一响应者

swift - 内存泄漏,尽管没有强引用?

ios - 禁用不同 ViewController 中的按钮 - Swift 3

ios - 带有自定义单元格的 UICollectionView, fatal error : unexpectedly found nil while unwrapping an Optional value

ios - 复合过滤器数组的 Realm 查询

swift - 如何将对象从子上下文检索到另一个上下文?