swift3字典限制命令由于信号而失败:分段错误:11

时间:2017-04-09 08:50:45

标签: ios swift xcode

(目前在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!
        }
    }
    

1 个答案:

答案 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
}