LoginSignup
1
4

More than 5 years have passed since last update.

Playgoundをもっと使いこなす!

Last updated at Posted at 2017-03-30
1 / 23

Swiftのちょっとしたコードを試せるPlaygound。

私はまだまだその真価を活かしきれないのかもしれません。


ちょっと便利になるPlaygroundのTipsを紹介したいと思います。


独自フレームワークをインポートする


自分で作ったフレームワークをインポートしたい時ありますよね?
ありますよね?


作りましょう。


Playgoundでフレームワークインポート

  1. アプリケーションプロジェクトを作成
  2. プロジェクトをワークスペースとして保存。
  3. そのアプリケーションプロジェクトの中にPlaygoundを作成
  4. ワークスペースにフレームワークプロジェクトを追加
  5. ApplicationのGeneralのEmbedded BinariesとLinked Frameworks and Librariesに該当フレームワークをインポートする

アプリケーションプロジェクトを作成

 2017-03-28 0.35.05.jpg


プロジェクトをワークスペースとして保存。

fig1.jpg


そのアプリケーションプロジェクトの中にPlaygoundを作成

File -> New -> File...からPlaygroundを選択

fig2.jpg

  • File -> New -> Playground だとプロジェクトに紐付かないプレイグランドが作成されるので注意

そのアプリケーションプロジェクトの中にPlaygoundを作成

 2017-03-25 18.04.38.jpg


ワークスペースにフレームワークプロジェクトを追加

fig4.jpg


ワークスペースにフレームワークプロジェクトを追加

Add to にワークスペースを指定する

fig6.jpg


ApplicationのGeneralのEmbedded BinariesとLinked Frameworks and Librariesに該当フレームワークをインポートする

 2017-03-25 18.12.00.jpg


インポートできた!

 2017-03-28 13.16.53.jpg

アプリケーション側でビルドが必要


PlaygoundでUITableView


PlaygoundでもUITableViewを表示できます!

20170328_1490676681.jpg


PlaygoundでUITableView

  1. PlaygroundSupportをインポート
  2. TableViewのモデルを作る
  3. TableViewを作成
  4. PlaygroundのLiveViewにTableViewを代入

PlaygroundSupportをインポート

import PlaygroundSupport


TableViewのモデルを作る

アプリの設定ページを想定してます

enum Section: String {
    case settiong = "設定"
    case support = "サポート"
    case info = "製品情報"

}

enum Row: String {
    case userSettiong = "ユーザー情報"
    case config = "アプリを設定"
    case faq = "よくある質問"
    case howToUse = "使い方"
    case site = "公式サイト"
    case privacyPoricy = "プライバシーポリシー"
    case term = "利用規約"
    case contact = "お問い合わせ"
}

struct SectionList {
    var type: Section
    var items: [Row]
}

let settingList = [
    SectionList.init(type: .settiong, items: [.userSettiong, .config]),
    SectionList.init(type: .support, items: [.faq, .howToUse, .site]),
    SectionList.init(type: .info, items: [.privacyPoricy, .term, .contact])
]


TableViewを作成

UITabieViewControllerのサブクラスを作成。
overrideでテーブルの設定をしていきます。

final class SettingTableViewController: UITableViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor.white
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return settingList[section].items.count
    }

    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 80.0
    }

    override func numberOfSections(in tableView: UITableView) -> Int {
        return settingList.count
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = UITableViewCell(style: .subtitle, reuseIdentifier: nil)
        let sectionItem = settingList[indexPath.section].items[indexPath.row]
        cell.textLabel?.text = sectionItem.rawValue
        return cell
    }

    override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        return settingList[section].type.rawValue
    }

    override func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
        view.tintColor = UIColor.gray
        let header = view as! UITableViewHeaderFooterView
        header.textLabel?.textColor = UIColor.white
    }
}




PlaygroundのLiveViewにTableViewを代入

needsIndefiniteExecutionをtrueにしてプレイグランドを無限に実行させる。
liveViewにTableViewを代入する

let viewController = SettingTableViewController()
viewController.title = "設定ページ"
let navigationController = UINavigationController(rootViewController: viewController)
navigationController.view.frame = CGRect(x: 0, y: 0, width: 320, height: 480)

PlaygroundPage.current.needsIndefiniteExecution = true
PlaygroundPage.current.liveView = navigationController.view


プレイグランドをまだまだ出来る子(๑•̀ㅂ•́)و✧

1
4
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
1
4