Need help with swiftpoet?
Click the “chat” button below for chat support from the developer who created it, or find similar developers for support.

About the developer

169 Stars 10 Forks Apache License 2.0 82 Commits 8 Opened issues


Kotlin and Java API for generating .swift source files.

Services available


Need anything else?

Contributors list


GitHub Workflow Status Maven Central Sonatype Nexus (Snapshots) codebeat badge

is a Kotlin and Java API for generating
source files.

Source file generation can be useful when doing things such as annotation processing or interacting with metadata files (e.g., database schemas, protocol formats). By generating code, you eliminate the need to write boilerplate while also keeping a single source of truth for the metadata.


Here's a

import RxSwift

class Greeter {

private let name: String

init(name: String) { = name }

func greet() -> Observable { return Observable.from("Hello (name)") }


And this is the code to generate it with SwiftPoet:

val observableTypeName = DeclaredTypeName.typeName("RxSwift.Observable")

val testClass = TypeSpec.classBuilder("Greeter") .addProperty("name", STRING, Modifier.PRIVATE) .addFunction( FunctionSpec.constructorBuilder() .addParameter("name", STRING) .addCode(" = name\n") .build() ) .addFunction( FunctionSpec.builder("greet") .returns(observableTypeName.parameterizedBy(STRING)) .addCode("return %T.from("Hello \(name)")\n", observableTypeName) .build() ) .build()

val file = FileSpec.builder("Greeter") .addType(testClass) .build()

val out = StringWriter() file.writeTo(out)

The KDoc catalogs the complete SwiftPoet API, which is inspired by JavaPoet.


Download the latest .jar or depend via Maven:


or Gradle:

compile 'io.outfoxx:swiftpoet:1.1.0'

Snapshots of the development version are available in Sonatype's



Copyright 2017 Outfox, Inc.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

We use cookies. If you continue to browse the site, you agree to the use of cookies. For more information on our use of cookies please see our Privacy Policy.