I'm wondering, if in any way, the following code can be improved or at least reduced:
func addCourse(cursoRecibido: Course) throws {
let curso = Course()
if let author = cursoRecibido.author {
curso.author = author
} else {
throw addCourseError.emptyAuthor
}
if let title = cursoRecibido.title {
curso.title = title
} else {
throw addCourseError.emptyTitle
}
if let duration = cursoRecibido.duration {
curso.duration = duration
} else {
throw addCourseError.emptyDuration
}
if let date = cursoRecibido.uploadDate {
curso.uploadDate = date
} else {
throw addCourseError.emptyUploadDate
}
if let views = cursoRecibido.views {
curso.views = views
}
dao.addCourse(curso)
}
The class of this method deals with the business logic to save the course in sqlLite.
You should start by asking yourself why you create a new course to add when you already have one in the function as a parameter. Maybe you should check that all the fields are filled in and then save it.
Still, there are many possible solutions. I'll give you two, one reducing your code, and the other how I would do it:
Solution 1
Solution 2