メモ > 技術 > IDE: Xcode > UIKit+Playground
UIKit+Playground
■Playgroundでボタンやラベルを確認
import UIKit
var myLabel = UILabel(frame: CGRect(x: 0, y: 0, width: 100, height: 30))
myLabel.backgroundColor = UIColor.gray
myLabel.text = "テスト"
■PlaygroundでJSONを取得
※あらかじめ
https://refirio.org/memos/ios/json_book.php
に以下のプログラムを用意している
<?php
$data = array(
'books' => array(
array(
'title' => 'C言語入門',
'price' => '1500'
),
array(
'title' => 'JAVA言語入門',
'price' => '1600'
),
array(
'title' => 'Ruby言語入門',
'price' => '2000'
)
)
);
echo json_encode($data);
exit;
import Foundation
import PlaygroundSupport
PlaygroundPage.current.needsIndefiniteExecution = true
class Client {
func someTask() {
let target = URL(string: "https://refirio.org/memos/ios/json.php")!
let task = URLSession.shared.dataTask(with: target) { data, response, error in
if let jsonData = data {
self.printJSON(jsonData)
}
}
task.resume()
}
func printJSON(_ data: Data) {
do {
let json = try JSONSerialization.jsonObject(with: data, options: .allowFragments)
print(json)
if let items = (json as AnyObject).object(forKey: "books") {
for item in items as! NSArray {
guard let title = (item as AnyObject).object(forKey: "title") else {
continue
}
guard let price = (item as AnyObject).object(forKey: "price") else {
continue
}
print(title)
print(price)
}
}
} catch {
print("parse error!")
}
}
}
let client = Client()
client.someTask()
■PlaygroundでViewControllerを使う
[iOS 10] PlaygroundでUIKitの描画を行う | Developers.IO
http://dev.classmethod.jp/smartphone/ios-10-playground-uikit-draw/