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
In the "Laotan Pickled Cabbage" inciden...
Car consumption was once an unattainable luxury f...
Account structure is both our tool to control tra...
Ideal Auto released its sales data for the 8th we...
Audit expert: Hu Qizhou Associate Professor, Scho...
Audit expert: Wang Guoyi Postdoctoral fellow in N...
Multiple media reported that Douyin Pay has been ...
Internet promotion, especially mobile Internet pr...
It is becoming increasingly difficult to operate ...
Grain Full, the eighth of the 24 solar terms, sym...
What is the first thing to do when you wake up in...
Beijing time, March 9 morning news, before the Al...
This article will use the Internet finance indust...
: : : : : : : : : : : : : : : .mp3 8.63M ├──【Free ...
In order to better penetrate into various industr...