gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 01:36:54 24 4
gpt4 key购买 nike

我正在尝试理解我正在从事的项目中的一些代码。我有一个字符串数组属性:变量名称:[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 时会发生什么,那个,会发生什么调用 .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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com