【问题标题】:Using switch to compare against an array of values使用 switch 与值数组进行比较
【发布时间】:2015-10-04 16:00:52
【问题描述】:

我创建了一个 Swift 2 函数,它接受输入字符串并将其转换为标题大小写,而不转换通常在标题中保持小写的小单词。代码在这里:

/*
Function: toTitleCase
Intent:   Take in String type and convert it to title case
*/
func toTitleCase(var inputString: String) -> String {

    // Convert entire input string to lower case first
    inputString = inputString.lowercaseString

    // A place holder for the string as it is being built
    var workString = ""

    // Set boolean to always convert the first word
    var isFirstWord = true

    // Get list of words from inputString
    let words = inputString.characters.split{$0 == " "}.map(String.init)

    for eachWord in words {
        switch eachWord {
            // If the word is in the list, do not convert it
            case "a","an","by","in","of","on","the","is","for","from":
                if isFirstWord {
                    fallthrough // If it is the first word, allow conversion
                } else {
                    workString = workString + " " + eachWord
                }
                // For any other word, convert it
            default:
                workString = workString + " " + eachWord.capitalizedString
        }
        isFirstWord = false
    }
    return workString
}

在 case 语句中,值是硬编码的,在这种情况下这可能不是最好的主意。由于将来我可能需要在此列表中添加更多单词,因此更理想的方法是将 plist 文件中的排除单词列表读取到数组中,并有这样的情况:

case [excludedWords]:
     (code to skip conversion)

显然这不起作用,因为您无法将 String 类型与 Array 类型进行比较。有没有其他方法可以轻松实现这个用例,还是我需要废弃它并使用类似 for-in 循环的东西?

有没有其他人开发过代码来实现这个逻辑,你能帮我找到一个更优雅、更有效的方法吗?

【问题讨论】:

    标签: arrays switch-statement swift2


    【解决方案1】:

    您可以像这样使用case let ... where 在排除的单词数组上使用contains

    func toTitleCase(var inputString: String) -> String {
    
        // Convert entire input string to lower case first
        inputString = inputString.lowercaseString
    
        // A place holder for the string as it is being built
        var workString = ""
    
        // Set boolean to always convert the first word
        var isFirstWord = true
    
        // Get list of words from inputString
        let words = inputString.characters.split{$0 == " "}.map(String.init)
    
        let excludedWords = ["a","an","by","in","of","on","the","is","for","from"]
    
        for eachWord in words {
            switch eachWord {
                // If the word is in the list, do not convert it
            case let word where excludedWords.contains(word):
                if isFirstWord {
                    fallthrough // If it is the first word, allow conversion
                } else {
                    workString = workString + " " + eachWord
                }
                // For any other word, convert it
            default:
                workString = workString + " " + eachWord.capitalizedString
            }
            isFirstWord = false
        }
        return workString
    }
    

    【讨论】:

    • 哇,这真是一个优雅的解决方案!非常感谢您的快速回复。
    【解决方案2】:

    这里有一个持久内存支持

    var excludedWords:[String] = ["a","an","by","in","of","on","the","is","for","from"]
    
    //store and retrive stack
    typealias plist = AnyObject
    var consistentExcludeList:plist?{ //guarenteed to be string 
    get{
        return excludedWords
    }
    set{
        if let excludedWordsArray = newValue as? [String]{
            for element in excludedWordsArray{
                excludedWords.append(element)
            }
        }
    }
    }
    let defaults = NSUserDefaults.standardUserDefaults()
    func saveStack(){
        defaults.setObject(consistentExcludeList, forKey: "consistentExcludeList")
    }
    func retrieveStack(){
        consistentExcludeList = defaults.objectForKey("consistentExcludeList")
    }
    
    //to titlecase
    func toTitleCase(inputString: String) -> String {
        var workString = ""
        let array = workString.componentsSeparatedByString(" ")
        for word in array{
            if excludedWords.contains(word){
                word.capitalizedString
            }
            workString += word
        }
        return workString
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-10-07
      • 2013-05-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-23
      • 2013-06-04
      相关资源
      最近更新 更多