ios - Swift Array 将 nil 的 var 传递给 .contains 和 .filter 时会发生什么

标签 ios arrays swift filter contains

我正在尝试理解我正在处理的项目中的一些代码。我有一个字符串数组属性: var names: [String]!

func findName(name: String?) -> [Name]? {
    if name != nil {
        return nameManager.namesForSearchString(name)?.filter({self.names.contains($0.name)})
    } else { 
        return nameManager.allNames.filter({self.names.contains($0.name)}) //<-what get's returned here?
    }
}

我不明白的是如果名称​​为 nil,当.contains被调用时会发生什么,以及that,什么当 .filter 被调用时会发生什么?这是在Favorites类中实现的,如果点击按钮,我需要调用这个函数来返回所有收藏夹,那么我将传递给这个函数什么来确保 Names: [Name] 的所有内容被退回?

在较低的层面上,我想了解 .contains 和 .filter 是如何工作的,以及如果将 nil 传递给它们会返回什么。

来自不同提交的相同方法的另一个版本(我也没有写)是这样的:

func findFavorites(name: String?) -> [Station]? {
        if name != nil {
            return nameManager.namesForSearchString(name)!.filter({contains(self.names, $0.objectId)})
        } else {
            return nameManager.allNames.filter({contains(self.names, $0.objectId)})
        }
    }

最佳答案

我不想发布非答案,但我确实希望其格式正确,所以我想评论是行不通的。这可能会帮助您了解发生了什么,以及过滤器/包含会发生什么。如果您还有其他问题,请告诉我,我会回答问题。如果我完全偏离基地,也请告诉我!

    // I don't know why this is implicitely unwrapped, as a nil in this Array crashes Playground execution
var localNames: [String!] = ["Troy", "Bob", "Donald"]

// I'm just modelling what I know about NameManager
struct NameManager {
  var allNames = [Name(name: "Bob"), Name(name: "Liz"), Name(name: "Anastasia")]
}

// I also assume the `name` in Name is a non-optional.
struct Name {
  var name: String = "some name"
}

var nameManager = NameManager()

func findName(name: String?) -> [Name]? {
  // Case where `name` is non-nil is excluded for demonstration purposes
  // I have expanded all the closure short-hands so we always see what we're doing.
  let allNames = nameManager.allNames
  // namesMatchingName is of type [Name], that we get by applying a filter.
  // `filter` works on a predicate basis: it goes through each element, one at a time,
  // and checks if it meets the "predicate", that is, a boolean 
  // condition that returns true or false. If it DOES meet the criteria, it will be included in
  let namesMatchingName = allNames.filter { (currentName) -> Bool in
    // Now we're inside the filter-predicate. What we do here is check if the `currentName`
    // is in `localNames`.
    let namesHasCurrentName = localNames.contains(currentName.name)
    // If the name IS in `localNames` we return true to the filter, 
    // which means it will be included in the final array, `namesMatchingName`.
    return namesHasCurrentName
  }
  // So now we have all the names that appear in both `nameManager.allNames` and `localNames`
  return namesMatchingName
}

findName(nil) // returns [{name: "Bob"}]

关于ios - Swift Array 将 nil 的 var 传递给 .contains 和 .filter 时会发生什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36161997/

相关文章:

python - 如何以编程方式从十六进制中检索 unicode 字符?

arrays - 在 swift 中将 var 声明为一个组

ios - 表达式太复杂,无法在合理的时间内解决 Swift 3

swift - 按键排序词典

ios - App Store 中两个同名的 iOS 应用程序

ios - 如何在 swift 中使用 MVVM 和 RxSwift 显示/隐藏 progressHUD

ios - 创建中间名和姓氏时的可选项

java - 如何复制二维字符串数组?

ios - Rx swift : using rx_refreshing for uirefreshcontrol

ios - ARC 中过度保留的调试策略?