避免许多如果条件



>我有 6 个字符串(文本、年份、年龄,....(我想根据它们过滤数组......我想之前检查字符串是否为空..如果不为空,则作为 &&.. 添加到 if 条件中。但是如何做这样的事情,而不需要在每个字符串上用彼此的字符串写很多 if 条件呢?

我想避免做这样的事情:

if self.searchtext != "" && self.qualification != ""{
}else if self.searchtext != "" && self.qualification != "" && self.gender != ""{
}else if self.searchtext != "" && self.qualification != "" && self.gender != "" && self.year != ""{
}else if self.searchtext != "" && self.qualification != "" && self.gender != "" && self.year != "" && self.major != ""{
}
....

怎么做?

if self.searchtext != ""{
if user.name.contains(find: self.searchtext) || user.mobile.contains(find: self.searchtext) || user.email.contains(find: self.searchtext){
DispatchQueue.main.async {
self.phones.append(user.mobile)
self.names.append(user.name)
}
}
}else if self.qualification != ""{
if user.qualification.contains(find: self.qualification){
DispatchQueue.main.async {
self.phones.append(user.mobile)
self.names.append(user.name)
}
}
}else if self.gender != ""{
if user.gender.contains(find: self.gender){
DispatchQueue.main.async {
self.phones.append(user.mobile)
self.names.append(user.name)
}
}
}else if self.year != ""{
if user.graduateYear.contains(find: self.year){
DispatchQueue.main.async {
self.phones.append(user.mobile)
self.names.append(user.name)
}
}
}else if self.major != ""{
if user.specialization.contains(find: self.major){
DispatchQueue.main.async {
self.phones.append(user.mobile)
self.names.append(user.name)
}
}
}else if self.city != ""{
if user.city.contains(find: self.city){
DispatchQueue.main.async {
self.phones.append(user.mobile)
self.names.append(user.name)
}
}
}
}

这就是我的工作..但是当我希望它像所有没有空字符串一样时,它可以作为OR工作

这是从LinkedIn的风格指南中复制的一段话

使用保护语句

3.11.1 一般来说,我们更喜欢在适用的情况下使用"早期返回"策略,而不是在if语句中嵌套代码。在此用例中使用 guard 语句通常很有帮助,并且可以提高代码的可读性。

// PREFERRED
func eatDoughnut(at index: Int) {
guard index >= 0 && index < doughnuts.count else {
// return early because the index is out of bounds
return
}
let doughnut = doughnuts[index]
eat(doughnut)
}
// NOT PREFERRED
func eatDoughnut(at index: Int) {
if index >= 0 && index < doughnuts.count {
let doughnut = doughnuts[index]
eat(doughnut)
}
}

这是我个人所做的,这是一个很好的约定,可以避免嵌套 if。

在您的情况下,您可以做几件事。首先,使用.isEmpty而不是将字符串与空字符串进行比较 (""(。如果您的唯一目的是检查是否所有字符串都为空,则可以像这样完成此操作:

let strings = [searchtext, qualification, ...]
guard strings.filter({ $0.isEmpty }).count == 0 else {
return
}
// Code that only works if all fields have values

我们当然可以简化很多事情。

我将首先将搜索条件包装到一个对象中:

struct UserSearchCriteria {
var searchText = ""
var gender = ""
var qualification = ""
...
init() {}
}

然后,您可以使用一个变量代替 X 变量:

var searchCriteria = UserSearchCriteria()

然后你可以添加一个简单的匹配方法:

extension UserSearchCriteria {
func matchesUser(_ user: User) -> Bool {
if !searchText.isEmpty && [user.name, user.mobile, user.email].contains(where: { $0.contains(find: searchText) }) { 
return true
}
if !qualification.isEmpty && user.qualification.contains(find: qualification) {
return true
}
...
return false
}
}

然后你的大病症就减少到:

if self.searchCriteria.matches(user) {
DispatchQueue.main.async {
self.phones.append(user.mobile)
self.names.append(user.name)
}
}

您无法真正避免这些条件,但您可以简化它们并更好地组织它们,而无需重复代码。

似乎您想匹配所有搜索条件,那么我将匹配方法更改为:

func matchesUser(_ user: User) -> Bool {
if !searchText.isEmpty && ![user.name, user.mobile, user.email].contains(where: { $0.contains(find: searchText) }) { 
return false
}
if !qualification.isEmpty && !user.qualification.contains(find: qualification) {
return false
}
...
return true
}

(注意双重否定 -contains条件否定并返回false(。

您可以使用 Guard 语句提前退出作用域。

guard 语句用于将程序控制转移到范围之外 如果不满足一个或多个条件。

仅当所有字段都不为空时,此操作才会通过。

guard self.searchtext != "", self.qualification != "", self.gender != "", self.year != "", self.major != "" else { return }

更新:

guard let searchText = self.searchtext, !searchText.isEmpty else { return }
// do something with `searchText`. Here the above conditions will be true. 

或者您可以使用if-let

if let searchText = self.searchtext, !searchText.isEmpty {
// do with `searchText`(non-optional). 
} else {
// conditions failed.
}

如果您尝试过滤数组,只需使用array.filter

let filteredUsers = usersArray.filter {
let shouldIAddThisElement = $0.name.contains(searchText) // Do your logic here
return shouldIAddThisElement
}

Swift 包含guard语句。仅当保护条件为 true 时,才会执行guard语句后面的行。与if/else语句一样,如果条件为 false,则运行else子句。

guard condition else {
// false: execute some code
}
// true: execute some code

对于您的情况:

guard self.searchtext && self.qualification && self.gender && self.year && self.major else { return }
// execute some code

return语句将退出函数或方法。

有关详细信息,请参阅"提前退出"部分的文档:

https://docs.swift.org/swift-book/LanguageGuide/ControlFlow.html

相关内容

最新更新