(目前在Xcode 8.2和swift3上)
一旦我更新到swift3,我就会出现此错误
"由于信号命令失败:分段错误:11"
因为我有大量keys
&单个字典中的values
。
当我挖掘出来时,我发现字典的键和值有限,当我添加超过特定数量的键时值,上面的错误显示出来。
如何使字典容纳更多的键和值?
import Foundation
open class TranslateDictionary {
var translateDictionary: [String: String] = [
"alphabetic": "one",
"categories": "two",
"levels": "three",
"mlevel1": "four",
"mlevel2": "five",
"mlevel3": "six",
"mlevel4": "seven",
"mlevel5": "eight",
"mlevel6": "nine",
//It contain a lot of keys and values not just those above!
]
func toArabic(_ label: String) -> String {
var arabic = translateDictionary[label]
if (arabic == nil) {
arabic = "NoTranslation"
}
return arabic!
}
}
答案 0 :(得分:1)
尝试插入这样的键:
var translateDictionary: [String: String] = {
var translateDictionary = [String: String]()
translateDictionary["key1"] = "value1"
translateDictionary["key2"] = "value2"
...
return translateDictionary
}()
您可能正在为字典文字命中大小限制。
顺便说一句,你的toArabic
函数会看起来像 Swifty 这样;#)
func toArabic(_ label: String) -> String {
guard let arabic = translateDictionary[label] else {
return "NoTranslation"
}
return arabic
}