使用 switch 与值数组进行比较

Using switch to compare against an array of values

我创建了一个 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{[=10=] == " "}.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 循环的东西?

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

您可以通过使用 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{[=10=] == " "}.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
}

这是一个支持持久内存的

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
}