【问题标题】:Comparing strings in an array to another array using switch case使用 switch case 将数组中的字符串与另一个数组进行比较
【发布时间】:2017-02-15 12:01:39
【问题描述】:

我是新来的,所以如果我没有提出正确的问题,请原谅我。我正在尝试创建一个函数,该函数将采用字符串数组(药物),然后通过将其与其他数组进行比较来判断它是否属于某些类别。我正在尝试使用案例切换方法来实现这一点。但它给了我错误“无法形成范围上限

如果有代码是:

//This is list of medications a patient may be on. This array will be generated by user input. 

var medicationArray = ["metoprolol", "Dulera", "Atrovastatin", "Albuterol", "lasix", "Sprinolactone", "Lisnopril","Aspirin","Apixaban"] 


//Function to compare medications above to arrays of different categories of medications. 
func medDetails(medications : [String]) {

//Arrays of list of different types of mjedications
let betaBlockerList = ["metoprolol", "carvedilol", "propanolol"]
let anticoagulantList = ["warfarin", "Apixaban","rivroxaban"]

var otherMedicationList : String = ""

// For loop to loop thru different medications patient is on. 
for medication in medications {

    //switch function to take the medication name and then comparing it against different arrays. 
    switch medication {
    //comparing medication against the range of of elements of first array. 
    case anticoagulantList[0]...anticoagulantList[anticoagulantList.count-1]:
        print("Patinet is on \(medication) for anticoagultion")

    //comparing medication against the range of of elements of second array. 
    case betaBlockerList[0]...betaBlockerList[betaBlockerList.count-1]:
        print("Patient is on \(medication) for betablocker")
    //list of medications that do not fit any of the above two categorias. 
    default:
        otherMedicationList = medication + ", "
        if medication == medications[medications.count - 1]{
            print("Patients other medications inculde \(otherMedicationList) .")

        }
    }
}
}

medDetails(medications: medicationArray

【问题讨论】:

  • 感谢相关。我之前审查过它。但是,它是相关帖子中的整数数组,而不是字符串。它也不能帮助我理解我遇到的错误。
  • 你只需要简单地使用case let x where array.contains(x)就可以了,不管数组是Int类型还是字符串类型。

标签: ios arrays swift switch-statement case


【解决方案1】:
 let betaBlockerList = ["metoprolol", "carvedilol", "propanolol"]

“betaBlockerList”的开关盒工作正常。它将从“m”到“p”的字符作为参数。这里这两个值是升序排列的。

let anticoagulantList   = ["warfarin", "Apixaban","rivroxaban"]

由于“(w)arfarin”和“(r)ivroxaban”的非升序,您的“anticoagulantList”开关盒无法正常工作

这里的切换案例不是将整个字符串作为参数。您的 betaBlockerList 案例也在执行以下所有值

var medicationArray = ["metoprolol", "n", "o"] 

【讨论】:

  • 谢谢。你能帮我了解一下字符串的升序是如何工作的,这样我就可以确保我不会再犯同样的错误了吗?
  • 按照@Nirav 所说的方式使用字符串搜索。
  • 当你要求进行字符串比较时,它会从左到右一个一个地检查字符。
【解决方案2】:

我认为切换并不是真正的最佳实践。一个好的方法是使用基于给定前提搜索或过滤数组的搜索功能。但是如果你想实现一个更天真的解决方案,只需执行两个 for 循环。一个用于药物,一个用于您要比较的另一个阵列。然后在循环中添加一个 if 语句,检查药物是否在该列表中,如果是,则您已找到答案,您可以在该点打破循环。

【讨论】:

  • 谢谢。我一定会尝试 if 方法。但我更想知道为什么会出现错误以及它是什么意思。
【解决方案3】:

@Nirav 已经对错误发表了评论,但问题是切换可能不是解决您的问题的最佳解决方案(例如,如果您有 300 个组怎么办?)

所以,这是一个只需要定义组的版本:

var medicationArray = ["metoprolol", "Dulera", "Atrovastatin", "Albuterol", "lasix", "Sprinolactone", "Lisnopril","Aspirin","Apixaban"] 

func medDetails(medications: [String]) {

    let input               = Set(medications)
    let betaBlockerList     = Set(["metoprolol", "carvedilol", "propanolol"])
    let anticoagulantList   = Set(["warfarin", "Apixaban","rivroxaban"])
    
    let groups = [
        "betablocker": betaBlockerList,
        "anticoagultion": anticoagulantList
    ]
    
    // Get rid of any element from input that is present in groups
    let unmatched = input.subtracting(groups.values.flatMap({$0}))
    
    for medication in input {
        for (groupName, groupValues) in groups {
            if groupValues.contains(medication) {
                print("Patient is on \(medication) for \(groupName)")
                break
            }   
        }
    }
    
    print("Patients other medications include: \(unmatched.joined(separator: ", "))")
}

当像 medDetails(medications: medicationArray) 这样调用时会打印出来

患者正在服用美托洛尔的 β 受体阻滞剂

患者正在使用阿哌沙班进行抗凝治疗

患者使用的其他药物包括:斯普林内酯、阿托伐他汀、Dulera、沙丁胺醇、阿司匹林、赖诺普利、lasix

【讨论】:

    猜你喜欢
    • 2014-08-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多