Swift flatMap gives unexpected result while using with optional array

后端 未结 3 951
半阙折子戏
半阙折子戏 2021-02-03 12:58

We have an array of the Person objects and each object has another array of String, which is optional. We want the consolidated list of car names in our society.



        
3条回答
  •  借酒劲吻你
    2021-02-03 13:31

    You need to call flatMap once more for array containing Optionals: personsArray.flatMap { $0.cars }.flatMap { $0 }

提交回复
热议问题