使用switch与值数组进行比较

使用switch与值数组进行比较

本文介绍了使用switch与值数组进行比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have created a Swift 2 function that takes an input string and converts it to title case, without converting small words that are normally kept lowercase in titles. The code is here:

/*
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文件中的排除单词列表读取到数组中,并遇到如下情况:

In the case statement, the values are hard coded, which in this situation is probably not the best idea. Since I will likely need to add more words to this list in the future, a more ideal approach would be to read the list of excluded words from a plist file into an array and have a case like this:

case [excludedWords]:
     (code to skip conversion)

显然,这将无法正常工作,因为您无法将String类型与Array类型进行比较.是否有另一种方法可以轻松实现此用例,还是我需要将其废弃并使用诸如for-in循环之类的东西?

Obviously this will not work since you cannot compare a String type to an Array type. Is there another way to easily implement this using case, or will I need to scrap it and use something like a for-in loop?

是否有其他人开发了实现此逻辑的代码,您能帮助我找到一种更优雅,更有效的方法吗?

Has anyone else developed code to implement this logic, and can you help me to find a more elegant and efficient way to do it?

推荐答案

您可以通过以下方式使用case let ... where在排除的单词数组上使用contains:

You could use contains on the array of excluded words by using case let ... where like this:

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
}

这篇关于使用switch与值数组进行比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 09:03