“This is the first day of my participation in the Gwen Challenge in November. See details of the event: The last Gwen Challenge in 2021”.

Hello, everyone, today is my first day to participate in the November update, today brings you the elegant use of chain call in Swift, the article is as follows:

The introduction

In daily development, when using Swift to make chained calls, code similar to the following is written:

class Person {
    var id: Int?
    var name: String?
    var age: Int?
    var nickName: String?

    func id(_ id: Int?). -> Person {
        self.id = id
        return self
    }

    func name(_ name: String?). -> Person {
        self.name = name
        return self
    }

    func age(_ age: Int?). -> Person {
        self.age = age
        return self
    }

    func nickName(_ nickName: String?). -> Person {
        self.nickName = nickName
        return self}}let person = Person().age(18).name("Bob").id(1).nickName("Job")
.
Copy the code

If only one or two classes need to implement the chain call, the problem is not very big. When we encounter a project in which almost all classes need to implement the chain call, it will not only be a large amount of code, but also too much repetitive work, time-consuming and exhausting, which will bring us great pain.

After experiencing this kind of pain for many times, I finally couldn’t stand this kind of writing method and wanted to find a new, cool and effort-saving way to implement chain invocation. After one or two hours of exploration, CNChainWrapper was born and I have joined the project now.

How to use

1, install,

CocoaPods

Add the following code to your Podfile, and the terminal executes Pod Install

pod 'CNChainWrapper'
Copy the code

Carthage

Stay tuned for

Swift Package Manager

Stay tuned for

2, use,

Import CNChainWrapper into the swift file you want to use

import CNChainWrapper
Copy the code

If you want to chain call, you just need to call CN first, and the code is as follows:

Class

Class that inherits from NSObject

class Student: NSObject {
    var id: Int?
    var name: String?
    var age: Int?
    var nickName: String?
}

let student = Student().cn
    .id(1)
    .name("Bob")
    .nickName("Job")
    .age(18)
Copy the code

Save time and effort by simply defining attributes for the class instead of implementing methods that return the class itself for each attribute

Classes that do not inherit from NSObject

Implementation method is similar to Struct, follow CNChainWrapperCompatible, refer to Struct

Struct

Struct needs special processing, it is not difficult, just need to comply with CNChainWrapperCompatible, code is as follows:

struct Child: CNChainWrapperCompatible {
    var id: Int?
    var name: String?
    var age: Int?
    var nickName: String?
}

let child = Child().cn
    .id(1)
    .name("Bob")
    .nickName("Job")
    .age(18)
Copy the code

Note: enum, has not been tested yet, and will be tested again when needed

Reference and thanks

  • RxSwifttherxImplementation method, the specific file isReactive.swift
  • The nuggets on this big brother article.