Xcodeでボタンを押すとラベルが変化するHello World的なものを作ってみた

やること

  1. Xcode開く
  2. Projectを作成する
  3. storyboardにLabelとButtonを置く
  4. LabelとButtonのリンクをViewControllerに貼る
  5. コード書いて終わり

Project作成まで

Xcodeを開き,Create a new Xcode Projectを選択する

f:id:MitubaEX:20171031005216p:plain

そのままNextを押す

f:id:MitubaEX:20171031005353p:plain

プロジェクト名などは適当に書く

f:id:MitubaEX:20171031005421p:plain

storyboard作成まで

Main.storyboardを選択し,Labelやボタンを配置する

f:id:MitubaEX:20171031005546p:plain

こんな感じ

f:id:MitubaEX:20171031005606p:plain

右上の円みたいなやつを押す

f:id:MitubaEX:20171031005847p:plain

storyboardとソースコードが並びます.

LabelやButtonを選択した状態で,Ctrlを押しながらコードの方までカーソルを持っていってリンクさせる
  • labelという名前で作成

f:id:MitubaEX:20171031010034p:plain

  • buttonという名前で作成

f:id:MitubaEX:20171031010220p:plain

buttonはconnectionをactionに変更する.

ソースコード変更

button関数の中身を以下のように変更する
@IBAction func button(_ sender: Any) {
        label.text = "hello"
}

完成

左上の再生ボタンのようなものを押して,実行してみる.

before

f:id:MitubaEX:20171031010519p:plain

after

f:id:MitubaEX:20171031010550p:plain

GOで書いたCLIツールをbrew installでinstallできるようにする

homebrew-<CLIツールのリポジトリ名>の設定

homebrew-<CLIツールのリポジトリ名>を作成してください.

そのリポジトリに以下の<CLIツールのリポジトリ名>.rbを追加してください

require 'formula'

HOMEBREW_<CLIツールのリポジトリ名(アッパーケース)>_VERSION='1.0'
class Filesmv < Formula
  homepage '<CLIツールのgithubのリポジトリURL(web)>'
  url '<CLIツールのgithubのリポジトリURL(clone時に使用する方)>', :tag => "master"
  version HOMEBREW_<CLIツールのリポジトリ名(アッパーケース)>_VERSION
  head '<CLIツールのgithubのリポジトリURL(clone時に使用する方)>', :branch => 'master'

  depends_on 'go' => :build

  def install
    ENV['GOPATH'] = buildpath
    system 'go', 'build', '-o', '<CLIツールのリポジトリ名>'
    bin.install '<CLIツールのリポジトリ名>'
  end
end

インストール

brew tap <githubユーザ名>/<CLIツールのリポジトリ名>
brew install <CLIツールのリポジトリ名>

これで自分の作ったCLIツールをインストールできました.

Kotlin+OkHttp3でjsonをPOSTする

build.gradle

build.gradleのdependenciesに以下を追加してください

compile 'com.squareup.okhttp3:okhttp:3.9.0'
compile group: 'org.json', name: 'json', version: '20170516'

Test.kt

以下のような感じでコードを書くと結果を受け取れます. 別途Nullチェックはするべきっぽいです.

import okhttp3.MediaType
import okhttp3.OkHttpClient
import okhttp3.Request
import okhttp3.RequestBody
import org.json.JSONObject

class Test(){
    fun post(): String?{
        val url = "url"
        val client: OkHttpClient = OkHttpClient.Builder().build()

        // create json
        val json = JSONObject()
        json.put("name", "mituba")
        json.put("hobby", "programming")

        // post
        val postBody = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), json.toString())
        val request: Request = Request.Builder().url(url).post(postBody).build()
        val response = client.newCall(request).execute()

        // getResult
        val result: String? = response.body()?.string()
        response.close()
        return result
    }
}

非常に簡単にPOSTすることができました.

spring+kotlinでJSONのリクエストとレスポンスをやってみた

Controller

package com.example.demo

import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.RequestBody
import org.springframework.web.bind.annotation.RestController

@RestController
class DemoController() {
    @GetMapping("/user")
    fun getUser(): User {
        val user = User(
            username = "grahamcox",
            screenName = "Graham"
        )
        return user
    }

    @PostMapping("/user")
    fun  registerUser(@RequestBody user: User): User {
        return user
    }
}

Userクラス

package com.example.demo

import com.fasterxml.jackson.annotation.JsonCreator

data class User @JsonCreator constructor(
    val username: String,
    val screenName: String
)

GETリクエス

http://localhost:8080/userにアクセスすると,{"username":"grahamcox","screenName":"Graham"}のようなJSONが返ってくる

POSTリクエス

下のようなリクエストをcurlで送る

curl --data '{"username":"test", "screenName":"mituba"}' -v -X POST -H 'Content-Type:application/json' http://localhost:8080/user

{"username":"test","screenName":"mituba"}のようなJSONが返ってくる

参考

参考にしました

scotch.io

kotlin+springをSpring Initializrを使ってサクッとhello worldまで

とりあえず今回は以下のバージョンで行いました

  • spring:2.0.0

Generate Projectを押すとzipファイルが落ちてきます. それを解答して,開発を始めます.

Controllerの追加

DemoApplicationが指定したgroupID, ArtifactIDの階層にあると思うので,そこと同じ階層にDemoControllerを作成します.

package com.example.demo

import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.RestController

@RestController
class CustomerController() {

    @GetMapping("/")
    fun hello() = "hello"

    @GetMapping("/{name}")
    fun helloName(@PathVariable name:String)
            = name
}

ルーティング

  • 今回は以下のようなルーティングにした
Method Route
GET /
GET /{name}

あとはgradle bootRunで起動する.http://localhost:8080にアクセスすると確認できる.