<commit_before>---
type: tutorial
layout: tutorial
title:  "Kotlin/Native as an Apple Framework"
description: "Compiling Kotlin/Native code and use it from Objective-C and Swift"
authors: <NAME>
date: 2018-08-05
showAuthorInfo: false
issue: EVAN-5132
---

Kotlin/Native provides bi-directional interoperability with Objective-C/Swift. 
Objective-C frameworks and libraries can be used in Kotlin code.
Kotlin modules can be used in Swift/Objective-C code too.
Besides that, Kotlin/Native has
[C Interop](https://github.com/JetBrains/kotlin-native/blob/master/INTEROP.md).
You may also want to take a look at the [Kotlin/Native as a Dynamic Library](dynamic-libraries.html)
tutorial.

In this tutorial, we will look at how to use Kotlin/Native code from
Objective-C and Swift applications on macOS and iOS.
We will build a framework from Kotlin code.

In this tutorial we'll: 
- [create a Kotlin Library](#creating-a-kotlin-library) and compile it to a framework
- examine the generated [Objective-C and Swift API](#generated-framework-headers) code
- use the framework from [Objective-C](#using-the-code-from-objective-c) and [Swift](#using-the-code-from-swift)
- [Configure XCode](#xcode-and-framework-dependencies) to use the framework for [macOS](#xcode-for-macos-target) and [iOS](#xcode-for-ios-targets)
   
## Creating a Kotlin Library

Kotlin/Native compiler can produce a framework for macOS and iOS
out of the Kotlin code. The created framework contains all declarations
and binaries needed to use it with Objective-C and Swift.
The best way to understand the techniques is to try it for ourselves. 
Let's create a tiny Kotlin library first and use it from an Objective-C program.

We create the `lib.kt` file with the library contents:

<div class="sample" markdown="1" mode="kotlin" theme="idea" data-highlight-only="1" auto-indent="false">

```kotlin
package demo

object Object {
  val field = "A"
}

interface Interface {
  fun iMember() {}
}

class Clazz : Interface {
  fun member(p: Int): Long? = 42
}

fun forIntegers(b: Byte, s: Short, i: Int, l: Long?) { }
fun forFloats(f: Float, d: Double?) { }

fun strings(str: String?) : String {
  return "That is '$str' from C"
}

fun acceptFun(f: (String) -> String?) = f("Kotlin/Native rocks!")
fun supplyFun() : FunFunction = { "$it is cool!" }
```
</div>

We need to have a Kotlin/Native compiler on our machines. 
You may want to take a look at the
[A Basic Kotlin/Native Application](basic-kotlin-native-app.html#obtaining-the-compiler)
tutorial for more information on performing this step.
Let's assume we have a console, where the `kotlinc` command is available. <commit_msg>fix(tutorial): fix a broken code in the K/N tutorial in the `apple-framework.md` file<commit_after>---
type: tutorial
layout: tutorial
title:  "Kotlin/Native as an Apple Framework"
description: "Compiling Kotlin/Native code and use it from Objective-C and Swift"
authors: <NAME>
date: 2018-08-05
showAuthorInfo: false
issue: EVAN-5132
---

Kotlin/Native provides bi-directional interoperability with Objective-C/Swift. 
Objective-C frameworks and libraries can be used in Kotlin code.
Kotlin modules can be used in Swift/Objective-C code too.
Besides that, Kotlin/Native has
[C Interop](https://github.com/JetBrains/kotlin-native/blob/master/INTEROP.md).
You may also want to take a look at the [Kotlin/Native as a Dynamic Library](dynamic-libraries.html)
tutorial.

In this tutorial, we will look at how to use Kotlin/Native code from
Objective-C and Swift applications on macOS and iOS.
We will build a framework from Kotlin code.

In this tutorial we'll: 
- [create a Kotlin Library](#creating-a-kotlin-library) and compile it to a framework
- examine the generated [Objective-C and Swift API](#generated-framework-headers) code
- use the framework from [Objective-C](#using-the-code-from-objective-c) and [Swift](#using-the-code-from-swift)
- [Configure XCode](#xcode-and-framework-dependencies) to use the framework for [macOS](#xcode-for-macos-target) and [iOS](#xcode-for-ios-targets)
   
## Creating a Kotlin Library

Kotlin/Native compiler can produce a framework for macOS and iOS
out of the Kotlin code. The created framework contains all declarations
and binaries needed to use it with Objective-C and Swift.
The best way to understand the techniques is to try it for ourselves. 
Let's create a tiny Kotlin library first and use it from an Objective-C program.

We create the `lib.kt` file with the library contents:

<div class="sample" markdown="1" mode="kotlin" theme="idea" data-highlight-only="1" auto-indent="false">

```kotlin
package demo

object Object {
  val field = "A"
}

interface Interface {
  fun iMember() {}
}

class Clazz : Interface {
  fun member(p: Int): Long? = 42
}

fun forIntegers(b: Byte, s: Short, i: Int, l: Long?) { }
fun forFloats(f: Float, d: Double?) { }

fun strings(str: String?) : String {
  return "That is '$str' from C"
}

fun acceptFun(f: (String) -> String?) = f("Kotlin/Native rocks!")
fun supplyFun() : (String) -> String? = { "$it is cool!" }
```
</div>

We need to have a Kotlin/Native compiler on our machines. 
You may want to take a look at the
[A Basic Kotlin/Native Application](basic-kotlin-native-app.html#obtaining-the-compiler)
tutorial for more information on performing this step.
Let's assume we have a console, where the `kotlinc` command is available. 
{'fraction_non_alphanumeric': 0.1064107342527022, 'fraction_numerical': 0.005963473723443906, 'mean_word_length': 4.482124616956078, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 12, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3129758', 'n_tokens_mistral': 1755, 'n_tokens_neox': 1633, 'n_words': 659}