LoginSignup
0
2

More than 3 years have passed since last update.

SwiftをPlaygroundsで書こう

Posted at

Playgroundsとは

PlaygroundsとはAppleが開発したSwiftの書き方を学べるmac/ipadアプリです。最近はmicro bitやLEGO、ドローンといったものとの連携も見られ始めています。教育的側面が強く子供向けというイメージがあるかもしれませんが、「ちょっとSwiftで試したいことがあるけどわざわざXCodeで確認するのもなー」といった時に重宝します。

UIViewControllerを書く

準備

「書く」という表現が適切かはわかりませんが。とりあえず、Xcodeを立ち上げて最初に開かれる状態を再現します。import PlaygroundSupportを最初にPlaygroundPage.current.liveView = ViewControlelr()を最後に加えなければいけません。

import UIKit
import PlaygroundSupport

class ViewControlelr: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        print("viewDidLoad")
    }
}

PlaygroundPage.current.liveView = ViewControlelr()

実行すると、それらしきものができていることがわかります。
スクリーンショット 2021-05-10 12.18.45.jpg

背景色を変更する

これはPlaygroundsならではというわけではありませんが。

import UIKit
import PlaygroundSupport

class ViewControlelr: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .gray
        print("viewDidLoad")
    }
}

PlaygroundPage.current.liveView = ViewControlelr()

背景色を変更し、先程の黒い画面がViewを表していることが確認されました。
スクリーンショット 2021-05-10 12.19.45.jpg

右下の赤く光っているボタンを押すと、メッセージが表示されます。

0
2
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
0
2