2

I have searched for this issue and I did not find any solution that is working for me using the latest version of Xcode and Swift. I use three arrays:

1. baseArray[Meal]: array filled with every meal. Not locally saved.

2. favoritesArray[Favorite]: with names of all favorite meals, locally saved by the user with NSKeyedArchiver.
3. filteredArray[Meal]: baseArray but filtered for searchterm. In code: 

    (filteredArray = baseArray.filter { $0.type == searchtext }}

I use the last array in the tableView. If they want to see all meals, the filteredArray is the same as the baseArray.

My question: how can I get the filteredArray that it has all favorite meals (so where Meal.title == Favorite.name). How do i combine three arrays?

I have tried a lot of options in the last week, but none has worked.. I hope You can help me out!!

1
  • filteredArray = baseArray.filter { $0.type == search text && favoritesArray.contains($0.type) }? (you may want to convert the favoritesArray to a Set first) Commented Nov 25, 2016 at 12:05

2 Answers 2

9

This does what you want:

struct Meal {
  let title: String
}

struct Favorite {
  let name: String
}

let meal1 = Meal(title: "Soup")
let meal2 = Meal(title: "Stew")
let meal3 = Meal(title: "Pizza")

let favorite1 = Favorite(name: "Stew")

let baseArray = [meal1, meal2, meal3]
let favoritesArray = [favorite1]

let favoriteNames = favoritesArray.map { $0.name }

let filteredArray = baseArray.filter { favoriteNames.contains($0.title) }
Sign up to request clarification or add additional context in comments.

2 Comments

Thx a lot! It is working; I already had a favoritesArray so i didn't need to declare it again. The favoriteNames does the trick!
Good answer, how to get the same result if let title: String? in Meal? How will you handle this here? let filteredArray = baseArray.filter { favoriteNames.contains($0.title****) } This is optional.
2

This is the solution for you if I correctly understand your question.

struct Meal {
    let name: String
}

struct Favorite {
    let name: String
}

let baseArray = [Meal(name: "Meal1"), Meal(name: "Meal2"), Meal(name: "Meal3")]

let favoritesArray = [Favorite(name: "Meal1")]

let searchText = "Meal3"

let filteredArray = baseArray.filter { $0.name == searchText }
print(filteredArray)
// [Meal(name: "Meal3")]

let combinedArray = baseArray.reduce(filteredArray) { array, element in
    // First condition check if the current meal (element) in contained in the favorites
    // Second checks if the favorite meal isn't already in the filteder array
    if favoritesArray.contains(where: { $0.name == element.name }) &&
        !filteredArray.contains(where: { $0.name == element.name }) {
        return array + [element]
    }
    return array
}

print(combinedArray)
// [Meal(name: "Meal3"), Meal(name: "Meal1")]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.