background Recently we are working on a blockchain-related wallet project. The new App uses a brand new technology stack. In Android, we use Kotlin+RxJava+Android Architecture Components, and in iOS, we use Swift+RxSwift. This article does not discuss the architecture of the App, but only discusses the features of Kotlin used in the project. In Android App, it is no exaggeration to say that more than 95% of our code is developed using Kotlin. Therefore, it is necessary to make a brief summary of the use of Kotlin in this stage. Kotlin features used: 1. Extension Function Kotlin allows developers to add new functions to a class without changing the existing class. This feature is called extension function. Let's take a simple example: if you want to close an I/O stream, you might write a utility method in Java.
For Kotlin, you can extend Closeable with a function closeQuietly().
After that, any class that implements the Closeable interface can use its own closeQuietly() method to close the stream. We no longer need that utility method. In the project, we use extension functions to encapsulate Glide, which greatly simplifies the use of Glide.
In addition, we use extension functions in many places. I also updated my Kolin tool library, which includes various utils and extensions. https://github.com/fengzhizi715/SAF-Kotlin-Utils 2. Trailing Closure I didn't understand this concept at first. By chance, I saw our friends write the following code when using RxBus:
I was confused at the time because I wrote RxBus and I remember that it didn't provide such a method. After clicking on the register() method, I found that register is like this:
Since Kotlin is used, the use of the register method can be simplified as follows:
Since the first parameter of register() is a method or a closure, you can move the method or closure to the outermost part. It will become what you see in the project:
This is the trailing closure, which makes the code look more concise. 3. Usage of with With is to use an object as a parameter of a function. In the function block, this can refer to the object. In the function block, you can directly call the method or property of the object.
An Adapter before using with
After using with, the function block can omit "content."
IV. Others This part is not a feature of Kotlin, but a tool I developed using Kotlin, such as the logging framework L and Retrofit's logging interceptor. These libraries were actually developed a long time ago, and their functions have been slightly upgraded recently. L's github address:
The github address of Retrofit log interceptor:
The effect diagram of the log interceptor:
Summarize Kotlin absorbs the advantages of many languages and has many exciting features compared to Java, which greatly improves development efficiency. The features introduced in this article are just a drop in the ocean. Next, I will sort out more Kotlin features used in projects. BTW, when I was writing this article, the first version of the domestic wallet had just been completed and the first round of testing began. |
>>: Make iPhone fingerprint unlocking more sensitive | Qinggong
Since the whole society entered the Internet era,...
News reports that my country's oil and gas dr...
Most people regard routine annual physical examin...
[[280756]] On the evening of October 29, WeChat P...
Course Description For many e-commerce designers,...
On May 15, 2021, the Tianwen-1 probe successfully...
Frequent zoonotic diseases in recent years have g...
Nezha 2 has swept the world at the box office, an...
Community operation is a complete commercial mark...
As a user, we have to aim at the optimization pro...
What does the promotion display of opening an acc...
Editor's note: Every change of MOTO in those ...
On June 30, India banned 59 Chinese apps for secu...
This product is the first series of children'...
Isn’t it that product sales have encountered a bo...