LoginSignup
7
8

More than 5 years have passed since last update.

ことクロ8:KotlinでiOSアプリ Tips1 おすすめ書籍

Last updated at Posted at 2015-11-27

"Kotlinでスマートフォン・クロスプラットフォーム開発に至るまでの試行錯誤記録"、略して、"ことクロ"シリーズ その8。今回は、私と横並びで、Kotlinでクロスプラットフォーム開発入門したい人向けに、RoboVM社のkotlin contestと、現時点でのおすすめ書籍2冊とを紹介:
スクリーンショット 2015-11-27 9.57.55.png

序 AdventCalendar向け(?)、Kotlin on RoboVMコンテスト開始

クローズドソースになるというニュースで、Kotlinお試しを躊躇していたRoboVMが、kotlin contestなるものを年末に実施するとのこと。内容は、この年末にKotlinでちょっとしたiOSアプリを書いて、(たぶん英語で)ブログ等に紹介記事を書くと、有償となったRoboVMのライセンスなどがもらえるかもよ、という感じのAdvent Calendar向けなもの。

エントリー方法等は:
https://robovm.com/kotlin-contest/

ついでに、RoboVM 1.11 released – experimental Bitcode support, iOS 9.1 and Kotlin!ということで、Kotlinで、iOS9.1の他、Bitcode経由でwatchOSやtvOSも開発できるようになったとのこと。

さて、年末は、書籍を斜め読みして時間節約しつつ、kotlinでクロスプラットフォーム開発に挑戦だ!
近時、AndroidStudioベースとなったクロスプラットフォーム開発環境RoboVMの紹介は次回以降に。

1) 書籍1 パンダ本 “Kotlin for Android Developers”

leanpubという電子書籍出版社から出版されている、Antonio Leiva氏の、“Kotlin for Android Developers”が11月中旬に出版されている。副題は、’Learn Kotlin in an easy way while developing an Android App’
お値段は2000円くらい(+若干の寄付)で、購入は以下から行える:

著書のAntonio Leiva氏は、ばりばりJavaでAndroidアプリ開発行いつつKotlinでAndroidブログを書いている人。ブログにも有用な情報があるし、ブログで会員登録(?)すると書籍の5章までをダウンロードできるらしいんだけど、この章のお勧めは8章以降かな。
例えば、非同期スレッドのKotlin的な扱いについての記述を抜粋:

“8.2 Performing the request out of the main thread
As you may know, HTTP are not allowed to be done in the main thread, it will throw an exception. This is because blocking the ui thread is a really bad practice...Anko provides a very easy DSL to deal with asynchrony, which will fit most basic needs. It basically provides an async function that will execute its code in another thread, with the option to return to the main thread by calling uiThread. Executing the request in a secondary thread is as easy as this:

async {
     Request(url).run()
     uiThread { longToast("Request performed") }
 }

このあたりの、Kotlinならではの書き方のイディオム集的にこの本は使えると思う。

(付記)今後は”Kotlin in Action”にも期待

JetBrains社の中の人が執筆中のKotlin書籍"Kotlin in Action"。まだMEAPで4章までしか書かれていないのだけど、いいところは、章立てとコードが公開されているところ。

例 : 第3章 DEFINING AND CALLING FUNCTIONS

3章.章立て
3.1. Collections in Kotlin
3.2. Making Functions Easier to Call
3.2.1. Named Arguments
3.2.2. Default Parameter Values
3.2.3. Getting Rid of Static Utility Classes: Top-level Functions and Properties
3.3. Adding Methods to Other People's Classes: Extension Functions and Properties
3.3.1. Utility Functions as Extensions
3.3.2. Extension Properties
3.4. Collection-Friendly Functions: Infix Calls and Varargs
3.4.1. Varargs
3.4.2. Infix calls
3.4.3. The Power of Extensions
3.5. Making Your Code Tidy: Local Functions and Extensions
3.6. Summary

章立てを見て興味深いを持ったコードを書籍のサイトからダウンロードしてお試しできる。
例えば、コレクション周りについて:

01改.kt
val set = hashSetOf(1, 7, 53)
val list = arrayListOf(1, 7, 53)
val map = hashMapOf(1 to "one", 7 to "seven", 53 to "fifty-three")

fun main(args: Array<String>) {
//追加
    println(set)
    println(list)
    println(map)
    println()
// 元コード みんなjavaのライブラリを使っているということいいたいんだろうね。
    println(set.javaClass)
    println(list.javaClass)
    println(map.javaClass)
}
実行結果@01改.kt
[1, 53, 7]
[1, 7, 53]
{1=one, 53=fifty-three, 7=seven}

class java.util.HashSet
class java.util.ArrayList
class java.util.HashMap

2) 書籍2 公式ツバメ本 "The Swift Programming Language"

こちらはAndroid界隈から来た人向けの情報。KotlinでAndroid開発というお題自体、まだ新しめなんだろうけど、RoboVMはJava/Kotlinで、Android開発に加え、iOSアプリ開発も可能にするもの。
コンテスト開催を機会に一気にiOS開発までやってしまうのもあり、だ。

当然ながら、webには、KotlinでiOSアプリ開発に関する情報はほとんどない。ということで、似たテイストであるSwiftでのiOSアプリ開発を参考にするのが吉。

Swiftやってる人には有名な話だが、アップル社のSwift関連ドキュメントは充実していて、かつ、無償で電子書籍としてまとめ読みできる。アクセスは、iTunesから:

SwiftでのiOSアプリ開発の情報は日本語でもいろいろあるけれども、コード眺めるだけでも、公式ツバメ本は落としておいて良いと思う。

日本語に翻訳されたiOSのドキュメントはこちら:
https://developer.apple.com/jp/documentation/

3)KotlinでRoboVMプロジェクト開始

次回以降は、KotlinでRoboVMの具体的なTipsを書いていこうと思う。

今回は、RoboVM 1.11で開始したKotlinプロジェクトの初期情報だけ:

① フォルダ構成 : スクリーンショット 2015-11-27 7.12.57.png

②iOS側メイン

Main.kt
package com.mycompany.myapp

import org.robovm.apple.foundation.NSAutoreleasePool
import org.robovm.apple.uikit.UIApplication
import org.robovm.apple.uikit.UIApplicationDelegateAdapter
import org.robovm.apple.uikit.UIApplicationLaunchOptions

class Main : UIApplicationDelegateAdapter() {

    override fun didFinishLaunching(application: UIApplication?, launchOptions: UIApplicationLaunchOptions?): Boolean {
        return true
    }

    companion object {
        @JvmStatic fun main(args: Array<String>) {
            val pool = NSAutoreleasePool()
            UIApplication.main<UIApplication, Main>(args, null, Main::class.java)
            pool.release()
        }
    }
}

やはり、名前空間apple.uikitの知識があった方がよさそうだね。

7
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
8