Created
July 19, 2014 14:28
-
-
Save takebayashi-ma/34c53a58975a94e814e4 to your computer and use it in GitHub Desktop.
SwiftでCLIコードを書く ref: http://qiita.com/BeMarble/items/f0008cc6e9a9f3c5cb37
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env xcrun swift -i | |
import Cocoa | |
class Alert { | |
func put(str :String) { | |
// アラートメッセージを出力 | |
var alert = NSAlert() | |
alert.messageText = str | |
alert.runModal() | |
} | |
} | |
let message = "Hello Swift" | |
var alert = Alert() | |
alert.put(message) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ sudo xcode-select -switch /Applications/Xcode6-Beta3.app/Contents/Developer |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ touch hello.swift | |
$ chmod +x hello.swift |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ ./hello.swift |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ touch alert.swift | |
$ chmod +x alert.swift |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ ./alert.swift |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ touch http_request.swift | |
$ chmod +x http_request.swift |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ ./http_request.swift |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env xcrun swift -i | |
var str = "Hello World" | |
// 標準出力 | |
println(str) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env xcrun swift -i | |
import Cocoa | |
class HttpRequest { | |
/** | |
* HTTP通信をして取得結果のBodyを返す | |
*/ | |
func get(urlStr :String) -> String { | |
var url = NSURL.URLWithString(urlStr) | |
var request = NSURLRequest(URL: url) | |
var response:AutoreleasingUnsafePointer<NSURLResponse?> = nil; | |
var error: AutoreleasingUnsafePointer<NSErrorPointer?> = nil; | |
var responseData = NSURLConnection.sendSynchronousRequest(request,returningResponse: response, error:nil) as NSData | |
if error != nil { | |
// HTTPエラー時の処理を書く | |
return "" | |
} else { | |
// HTTP通信成功処理を書く | |
var responseStr:NSString = NSString(data:responseData, encoding:NSUTF8StringEncoding) | |
// 取得結果を返す | |
return responseStr | |
} | |
} | |
} | |
var urlStr = "http://www.yahoo.co.jp" | |
var http_request = HttpRequest() | |
var response = http_request.get(urlStr) | |
// 結果を標準出力 | |
println(response) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment