• December 28, 2020
  • July 29, 2021

Introduction to Combine | Connecting to the Web Server with the Combine

This article explains that how to connect to the web server with the Combine. Create the Publisher of the DataTask Create the publisher of the data task to connect to the web server. Two methods are available to create it: func dataTaskPublisher(for: URL) -> URLSession.DataTaskPublisher func dataTaskPublisher(for: URLRequest) -> URLSession.DataTaskPublisher The first method has an argument “URL”. If you want to simply connect to the URL and GET the contents, you can use this method. The second method has an argument “URLRequest”. With “URLRequest”, you can set the method of the HTTP, the body data, the header value and so […]

  • December 27, 2020
  • July 29, 2021

Introduction to Combine | Supporting Combine in a custom class

Combine is a framework to implement asynchronous operations. It is created by Apple, it is also used in Swift Data Binding. It is an important framework. Adopt to ObservableObject protocol. To make a class to support Combine, adopt to ObservableObject protocol. To notify a changes of properties with Combine, mark the property with @Published attribute. class PlayerStatus : ObservableObject { @Published var name: String = "" @Published var hitPoint: Int = 100 } @Published The property which is marked with @Published attribute, can make the Publisher. Use $ operator to make the Publisher. let status = PlayerStatus() let hitPointPublisher = […]