【发布时间】: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