使用switch来比较值数组

时间:2015-10-04 16:00:53

标签: arrays switch-statement swift2

我创建了一个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循环的东西?

是否还有其他人开发了实现此逻辑的代码,您是否可以帮助我找到更优雅高效的方法?

2 个答案:

答案 0 :(得分:3)

您可以使用此contains对排除字词数组使用case let ... where

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
}

答案 1 :(得分:0)

这是一个持久性内存支持

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
}