简体   繁体   中英

Swift Array what happens when passing var that is nil to .contains and .filter

I'm trying to understand some code in a project I'm working on. I have an array property of strings: 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?
    }
}

What I don't understand is if the name is nil, what happens when .contains is called, and with that , what happens when .filter gets called? This is implemented in a Favorites class, and I need to call this function to return all favorites if a button is tapped, so what would I pass to this function to ensure that all the contents of Names: [Name] are returned?

On a lower level, I want to understand how .contains and .filter work and what gets returned if nil is passed to them.

Another version of the same method from a different commit (that I also did not write) is this:

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 want to post a non-answer, but I do want this to be properly formatted so I guess a comment won't do. This might help you understand what's going on, and what happens with filter/contains. If you have any more questions, let me know, and I'll answer the question. If I'm completely off-base, let me know as well!

    // 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"}]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM