Difficulty: Beginner | Easy | Normal | Challenging
This article has been developed using Xcode 12.1, and Swift 5.3
If you want to pass data around your App, more than likely you will need to use a Data Transfer Object (DTO) to do so
- You will be expected to be aware how to make a Single View Application in Swift.
Data Transfer Object (DTO): An object that carries data between processes
It turns out I've been using Data Transfer Objects (DTO) without even knowing it! The codable protocol gives us a great way accessing API services.
The example code (which uses my Network Manager) calls https://jsonplaceholder.typicode.com/todos/1
means that this can look like the following:
struct ToDo: Codable {
let userId: Int
let id: Int
let title: String
let completed: Bool
}
similarly NSManagedObject
subclass can be used for Core Data implementations.
This means that we might like to create a toEntity
function to return the entity. An example?
extension ToDo {
func toEntity(in context: NSManagedObjectContext) -> ToDoEntity {
let entity: ToDoEntity = .init(context: context)
entity,userId = userId
entity.id = id
entity.title = title
entity.completed = completed
return enttity
}
}
Some people like to separate out the idea of an entity from a DTO by using a suffix (perhaps a DTO suffix), but this can be an overkill for some projects and implementation / this is an individual choice for the programmer involved.
We might think of these as DTO
in order to decouple our network layer from the UI and other modules. Inevitably Martin Fowler has an article on this if you want to read into this topic in more detail.
If you've any questions, comments or suggestions please hit me up on Twitter