Search code examples
swifttuplesvapor

Can I give back nil instead of a tuple?


My first version of code worked well:

return Question.find(o.questionId, on: req.db).map { q in
   guard let q = q else {
      return nil
   }
   return GetUserQuestionsOut(text: q.text, questionId: q.id!)
}

But if I work with tuples I got that 'nil' is incompatible with the return type:

return Question.find(o.questionId, on: req.db).map { q -> (String, GetUserQuestionsOut) in
guard let q = q else {
  return nil
} ...

Can I / How can I give back nil instead of a tuple?


Solution

  • Did you try this,

    ... { q -> (String?, GetUserQuestionsOut?) in
    
      }
    

    or

     ... { q -> (String, GetUserQuestionsOut)? in
    
      }
    

    on the other hand, maybe you could consider using struct(model) or typealias instead of tuple