Swift勉強会~NEWSアプリを作ろう~
SwiftTest.playground
import UIKit //変数の作り方 var name : String var num :Int //初期値有り var string = "AAA" var i = 10 //定数 let const = "固定値" //配列作成 var animals = ["ウサギ","ネコ","犬"] animals[1] //辞書(key:value) var scores = ["TARO":80,"JIRO":100,"SABURO":50] scores["TARO"] scores.count //関数作成 func sayHello(name:String){ print("Hello♥" + name) } //関数呼び出し sayHello("TARO") //関数(引数、戻り値あり) func sum(a:Int, b:Int) -> Int{ return a + b } //関数呼び出し print(sum(3, 5))
MyFirstApp
サンプルコード https://www.dropbox.com/s/3so0t3pjfurxc4g/MyFirstApp.zip?dl=0
TableTest(Playground)
import UIKit class MyContoller:UITableViewController{ override func viewDidLoad() { super.viewDidLoad() self.tableView.registerClass(UITableViewCell.self,forCellReuseIdentifier:"cell") } override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return 10 } override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { var cell = tableView.dequeueReusableCellWithIdentifier("cell") as UITableViewCell cell.textLabel.text = String((indexPath.row)+1) + "行目" return cell } } var controller = MyContoller() controller.view
JSONReader(Playground)
import UIKit import XCPlayground var urlString = "http://ajax.googleapis.com/ajax/services/feed/load?v=1.0&q=http://rss.dailynews.yahoo.co.jp/fc/rss.xml&num=10" var url = NSURL(string: urlString) //NSURLSessionでダウンロードする var task = NSURLSession.sharedSession().dataTaskWithURL(url!, completionHandler: {data, response, error in //JSONデータを辞書に変換する var dist = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: nil)as NSDictionary var responseData = dist["responseData"]as NSDictionary var feed = responseData["feed"]as NSDictionary var entries = feed["entries"]as NSArray for entry in entries{ println(entry["title"]) } //println(dist) }) task.resume() XCPSetExecutionShouldContinueIndefinitely(continueIndefinitely: true)
Newsリーダー
サンプルコード https://www.dropbox.com/s/s4uwdoq6ui2djg7/SwiftNews.zip?dl=0