[OSX]Swift標準入出力

<p>スクリプト言語の代わりにcommand-line環境でSwiftを利用するため、引数の取得や標準入出力について調べてみた。</p>

<p>引数の取得は以下の通り。</p>

```swift
let args = Process.arguments
for arg in args {
    print(arg)
}
```

<p>標準入力からreturnされるまでを読み込むのは以下の通り。</p>

```swift
let standardInput = NSFileHandle.fileHandleWithStandardInput()
let input = standardInput.availableData
let datastring = String(NSString(data:input, encoding:NSUTF8StringEncoding))
print(datastring)
```

<p>return単位で読み込み続ける場合は、以下となる。</p>

```swift
let standardInput = NSFileHandle.fileHandleWithStandardInput()
while true {
    let input = standardInput.availableData
    if input.length == 0 { break }
    let datastring = String(NSString(data:input, encoding:NSUTF8StringEncoding))
    print(datastring)
}
```

<p>上記は、空行が来るとループを抜ける。</p>

<p>標準出力への印字は以下の通り。</p>

```swift
let standardOutput = NSFileHandle.fileHandleWithStandardOutput()
let outputString: NSString = "hello\n"
let dataout: NSData? = outputString.dataUsingEncoding(NSUTF8StringEncoding)
if let dout = dataout {
    standardOutput.writeData(dout)
}
```

<p>同じ文字列を標準エラー出力に印字する場合。</p>

```swift
let standardError = NSFileHandle.fileHandleWithStandardError()
if let dout = dataout {
    standardError.writeData(dout)
}
```

<p>後で流用できるよう、これらを全部つなげた完全なスクリプトを載せる。</p>

```swift
#!/usr/bin/env xcrun swift
 
import Foundation
 
let args = Process.arguments
for arg in args {
    print(arg)
}
 
let standardInput = NSFileHandle.fileHandleWithStandardInput()
//while true {
    let input = standardInput.availableData
    // if input.length == 0 { break }
    let datastring = String(NSString(data:input, encoding:NSUTF8StringEncoding))
    print(datastring)
//}
 
let standardOutput = NSFileHandle.fileHandleWithStandardOutput()
let outputString: NSString = "hello\n"
let dataout: NSData? = outputString.dataUsingEncoding(NSUTF8StringEncoding)
if let dout = dataout {
    standardOutput.writeData(dout)
}
 
let standardError = NSFileHandle.fileHandleWithStandardError()
if let dout = dataout {
    standardError.writeData(dout)
}
 
/* End Of File */
```

関連情報
<a href="http://qiita.com/advent-calendar/2015/cocoa/">Cocoa Advent Calendar 2015</a><br />
<a href="https://developer.apple.com/swift/blog/">Swift Blog</a><br />
<a href="https://itunes.apple.com/us/book-series/swift-programming-series/id888896989?mt=11">Swift Programming Series (iBooks Store)</a><br />
<a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/index.html">The Swift Programming Language</a><br />
<a href="https://developer.apple.com/library/prerelease/ios//documentation/General/Reference/SwiftStandardLibraryReference/index.html">Swift Standard Library Reference</a><br />
<a href="https://developer.apple.com/library/prerelease/ios/referencelibrary/GettingStarted/DevelopiOSAppsSwift/index.html">Start Developing iOS Apps</a><br />
<a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/BuildingCocoaApps/index.html">Using Swift with Cocoa and Objective-C</a><br />

【Cocoa練習帳】
<a href="http://www.bitz.co.jp/weblog/">http://www.bitz.co.jp/weblog/</a><br />
<a href="http://ameblo.jp/bitz/">http://ameblo.jp/bitz/</a>（ミラー・サイト）<br />
<a href="https://t.co/6P3h2jtbRk</a>note<br />