LoginSignup
30
29

More than 3 years have passed since last update.

Swiftをコマンドラインで実行してWebAPIからjsonを取得する

Last updated at Posted at 2014-06-12

概要

このコードを参考に、一部分をコマンドラインで実行できるようにした。
SwiftでWebAPIアプリを作った時の要点 - Qiita

ターミナルからswiftをREPLで起動させてコードをコピペすると、TiqavのWebAPIをたたいてjsonを取得する。

gif

swift_repl.gif

コマンドラインからSwiftを実行する方法

$ sudo xcode-select -switch /Applications/Xcode6-Beta.app/Contents/Developer
$ xcrun swift 

ios - How can I use swift in Terminal? - Stack Overflow

実行するコード

import Foundation
// Thanks to tiqav api! ( http://dev.tiqav.com/ )
var request = NSURLRequest(URL: NSURL(string: "http://api.tiqav.com/search/random.json"))
var data = NSURLConnection.sendSynchronousRequest(request, returningResponse: nil, error: nil)
var json: NSArray = NSJSONSerialization.JSONObjectWithData(data, options:NSJSONReadingOptions.AllowFragments, error: nil) as NSArray
for item: NSDictionary! in json {
  println(item.objectForKey("source_url"))
}

実行結果

$ sudo xcode-select -switch /Applications/Xcode6-Beta.app/Contents/Developer
$ xcrun swift
Welcome to Swift!  Type :help for assistance.
  1> import Foundation
  2> // Thanks to tiqav api! ( http://dev.tiqav.com/ )
  3> var request = NSURLRequest(URL: NSURL(string: "http://api.tiqav.com/search/random.json"))
request: NSURLRequest = {}
  4> var data = NSURLConnection.sendSynchronousRequest(request, returningResponse: nil, error: nil)
data: NSData! = 3411 bytes
  5> var json: NSArray = NSJSONSerialization.JSONObjectWithData(data, options:NSJSONReadingOptions.AllowFragments, error: nil) as NSArray
json: __NSArrayI = @"30 objects" {
  [0] =
  [1] =
  [2] =
  [3] =
  [4] =
  [5] =
  [6] =
  [7] =
  [8] =
  [9] =
  [10] =
  [11] =
  [12] =
  [13] =
  [14] =
  [15] =
  [16] =
  [17] =
  [18] =
  [19] =
  [20] =
  [21] =
  [22] =
  [23] =
  [24] =
  [25] =
  [26] =
  [27] =
  [28] =
  [29] =
}
  6> for item: NSDictionary! in json {
  7.       println(item.objectForKey("source_url"))
  8. }   
http://apr.2chan.net/may/b/src/1259575702179.jpg
http://feb.2chan.net/jun/b/src/1259078675620.jpg
http://aug.2chan.net/jun/b/src/1288702377863.jpg
http://sep.2chan.net/jun/b/src/1331959816500.jpg
http://jun.2chan.net:81/b/src/1257440956875.jpg
http://apr.2chan.net/jun/b/src/1289270827260.jpg
http://sep.2chan.net/jun/b/src/1287737625271.jpg
http://feb.2chan.net/jun/b/src/1287740142921.jpg
http://jan.2chan.net/jun/b/src/1287221271145.png
http://mar.2chan.net/jun/b/src/1333033557075.jpg
http://apr.2chan.net/jun/b/src/1260208494697.jpg
http://jan.2chan.net/jun/b/src/1287570536309.jpg
http://aug.2chan.net/jun/b/src/1289782701602.png
http://jan.2chan.net/jun/b/src/1357143159406.jpg
http://apr.2chan.net/may/b/src/1259689152133.jpg
http://dec.2chan.net:81/b/src/1259397315084.jpg
http://aug.2chan.net/jun/b/src/1289740533304.jpg
http://jan.2chan.net/jun/b/src/1299366180429.jpg
http://apr.2chan.net/may/b/src/1259008684768.jpg
http://aug.2chan.net/jun/b/src/1338397857881.jpg
http://apr.2chan.net/jun/b/src/1299242054894.jpg
http://jan.2chan.net/jun/b/src/1260334891825.jpg
http://feb.2chan.net/jun/b/src/1299191757914.jpg
http://feb.2chan.net/jun/b/src/1258769251502.jpg
http://jan.2chan.net/jun/b/src/1287214040790.jpg
http://apr.2chan.net/jun/b/src/1332569014437.jpg
http://sep.2chan.net/jun/b/src/1288699390255.gif
http://apr.2chan.net/jun/b/src/1338451261333.jpg
http://apr.2chan.net/may/b/src/1258776572369.jpg
http://feb.2chan.net/jun/b/src/1257779417222.jpg
  9> 
30
29
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
30
29