Summary of experiences in developing Mac OS X applications or shared libraries in AppCode

Source: Internet
Author: User

AppCode is an integrated development environment produced by JetBrains for developing Mac OS X and iOS applications. It can support the running and debugging functions of Xcode projects, and provide extensive technical support for Swift and Objective-C. At present, AppCode can only run under the Mac OS X operating system environment. As an advanced alternative to XCode, AppCode inherits many excellent features of IDEA and is another weapon for Mac OS X and iOS application development.
Kotlin Native is a binary technology that can compile Kotlin source code without any virtual machine support. The compiled binary data can be run directly on the target platform. Kotlin Native introduced support for calling Objective-C API in version 0.4, and introduced experimental support for WebAssembly. To use Kotlin to develop iOS applications, you need to download the latest version of AppCode (ie AppCode 2018.1.1 and above), and then install the [Kotlin Native for AppCode] plugin.
The installation steps are: select [AppCode] → [Preferences] → [Plugins] in turn to open the plugin page of JetBrains, and then search for [Kotlin Native for AppCode] and install it, as shown in Figure 16-6.

Create Kotlin Native project
Kotlin Native uses Gradle as the default build tool. To compile a Kotlin Native project in Xcode, you need to add a relevant run script to the project. The script calls Gradle to build the Kotlin Native project. Of course, you can also use some third-party IDEs, such as AppCode, you need to install the [Kotlin Native for AppCode] plugin before using AppCode.
Start AppCode and select [Kotlin / Native] → [Single View APP with a Kotlin / Native Framwork] in turn to create an iOS application. IOS projects created using Kotlin / Native support Kotlin language to write iOS applications, as shown Shown in 16-7.

Waiting for the completion of the project construction, you can see that the directory structure of the iOS project created using Kotlin / Native is shown in Figure 16-8.

Compared to native iOS projects created using Xcode, there are obviously more configuration files (such as Frameworks and KotlinNativeFramework) for iOS projects created using Kotlin / Native. The specific meaning of the file is as follows:

Projects: Mainly used to store executable files for Mac software development, etc. This file is rarely used in iOS application development;
Frameworks: mainly used to store some system libraries and third-party libraries that the project depends on;
Project: This directory is used to store source code related to the project and is also a core component of iOS application development;
KotlinNativeFramework: Used to store libraries related to KotlinNative.

At this point, just click the [Run] button to start the application in the iPhone simulator or real device. However, the program at this time has not implemented any functions. In order to use Kotlin to write iOS applications in the project, you can create a Kotlin file directory (such as src / main / kotlin) in the Project file of the Kotlin Native project. Add a ViewController class, as shown in Figure 16-9.

It should be noted that the Kotlin Native project created by AppCode does not introduce the UIKit.framework system library of iOS by default, so you need to import the corresponding library before using the iOS related library.
The source code of the ViewController class is as follows:
import kotlinx.cinterop.ExportObjCClass
import kotlinx.cinterop.ObjCAction
import kotlinx.cinterop.ObjCOutlet
import platform.Foundation.NSCoder
import platform.UIKit. *
import kotlinx.cinterop.initBy

@ExportObjCClass
class ViewController: UIViewController {

    constructor (aDecode: NSCoder): super (aDecode)
    override fun initWithCoder (aDecode: NSCoder) = initBy (ViewController (aDecode))

    @ObjCOutlet
    lateinit var label: UILabel

    @ObjCOutlet
    lateinit var textField: UITextField

    @ObjCOutlet
    lateinit var button: UIButton

    @ObjCAction
    fun click () {
        label.text = "Hello, $ {textFiled.text}"
    }
}

Copy the code Since the file directory defined by the developer is not marked as the source root directory, it will not be recognized by the compilation system, so you need to mark the "src / main / kotlin" directory as the project source directory to be recognized by the system.
## Kotlin Native test
As we all know, AppCode itself is a Kotlin plugin based on IntelliJ IDEA, so many functions that support Kotlin are also supported for Kotlin / Native (such as code inspection, refactoring and other operations).

At the same time, the AppCode plugin also supports the use of kotlin.test framework to write test code. Right-click on the project name, select [Project Settings ...] → [Project Name] in turn, and then click the add button in the lower left corner, as shown in Figure 16-11.

Wait for the project to be built, then select [edit configurations ...] to open the project configuration panel and add the "Kotlin / Native test" option to add test cases, as shown in Figure 16-12.

Of course, in addition to the functions described above, Kotlin Native also supports calling Objective-C standard APIs, and it also provides a very convenient way to read API documentation. Kotlin Native is the real charm of Kotlin multi-platform.

Author: xiangzhihong
Link: https://juejin.im/post/5b513b9df265da0f7c4fc320
Source: Nuggets
The copyright belongs to the author. For commercial reproduction, please contact the author for authorization, and for non-commercial reproduction, please indicate the source.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.