0

So I have a struct that I am using to populate certain arrays.

    let mcdonalds = restaurant(name: "Mcdonalds")
    let burgerking = restaurant(name: "Burger King")
    let vinsetta = restaurant(name: "Vinsetta Garage")
    let chinaone = restaurant(name: "China One Buffet")
    let pandaexpress = restaurant(name: "Panda Express")
    let olivegarden = restaurant(name: "Olive Garden")
    let salvatore = restaurant(name: "Salvatorre Scallopinis")


    american = [mcdonalds, burgerking]
    burgers = [vinsetta]
    chinese = [chinaone, pandaexpress]
    italian = [olivegarden, salvatore]
    allRestaurantTypeArray = [american, burgers, chinese, italian]

unfortunately, when I attempt to combine my american,burgers,chinese, & italian arrays into the allrestaruranttypearray, I am greeted with a "cannot assigned a value of type "[[restaurant]]" to a value of type 'NSArray'".

would you be able to recommend any changes?

update:

enter image description here

4
  • You should name your structs starting with a capital letter Commented Oct 18, 2015 at 23:52
  • would that change anything? or just better code syntax? Commented Oct 18, 2015 at 23:54
  • There is no better syntax. It is a convention. Commented Oct 18, 2015 at 23:55
  • [american, chinese, burgers].flatmap{$0} Commented Oct 19, 2015 at 16:39

1 Answer 1

1

You probably want to do a flatMap here ... enter image description here

You are making an array of arrays which is what [[restaurant]] means.

flatMap will "flatten" them out.

struct Restaurant {
    var name = ""
}
let mcdonalds = Restaurant(name: "Mcdonalds")
let burgerking = Restaurant(name: "Burger King")
let vinsetta = Restaurant(name: "Vinsetta Garage")
let chinaone = Restaurant(name: "China One Buffet")
let pandaexpress = Restaurant(name: "Panda Express")
let olivegarden = Restaurant(name: "Olive Garden")
let salvatore = Restaurant(name: "Salvatorre Scallopinis")


let american = [mcdonalds, burgerking]
let burgers = [vinsetta]
let chinese = [chinaone, pandaexpress]
let italian = [olivegarden, salvatore]
let allRestaurantTypeArray = [american, burgers, chinese, italian]

let flattened = allRestaurantTypeArray.flatMap{$0}



flattened.first?.name  // "Mcdonalds"
flattened.last?.name   // "Salvatorre Scallopinis"

I've updated it to help you out even better:

enter image description here

You have to apply the flatmap to the array of arrays

Sign up to request clarification or add additional context in comments.

8 Comments

I know flatmap works with numbers, but can i use this function to flatten arrays with struct instances or strings?
@jonpeter my example is strings
@jonpeter look at the comment at another question of yours stackoverflow.com/a/33140261/2303865
I attempted that method but was unsuccessful. I wanted to try a different possibility, so I thought this attempt would be better. Which solution would you recommend @LeoDabus?
The fllatMap comment . Same as suggested by jeef
|

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.