Tuesday, May 23, 2017

Swift: Reading from Standard Streams (標準ストリーム)

Command:

$ cat ./Desktop/StandardInput/StandardInput/main.swift


Result:

import Foundation

var string = ""

func showMainMenu(){
    print("1) Walk")
    print("2) Study")
    print("3) Work")
    print("4) Sleep")
    print("> ", terminator: "")
}

func walk(){
    var line = ""
   
    repeat {
        print("Walking...")
        print("Enter 'exit' to finish")
        print("> ", terminator: "")
        line = readLine()!
    } while line != "exit"
}

func work(){
    var line = ""
   
    repeat {
        print("Working...")
        print("Enter 'exit' to finish")
        print("> ", terminator: "")
        line = readLine()!
    } while line != "exit"
}

func study(){
    var line = ""
   
    repeat {
        print("Studying...")
        print("Enter 'exit' to finish")
        print("> ", terminator: "")
        line = readLine()!
    } while line != "exit"
}

func sleep(){
    var line = ""
   
    repeat {
        print("Sleeping...")
        print("Enter 'exit' to finish")
        print("> ", terminator: "")
        line = readLine()!
    } while line != "exit"
}

showMainMenu()

while let line = readLine(){
    switch line{
    case "1":
        walk()
        break
    case "2":
        study()
        break
    case "3":
        work()
        break
    case "4":
        sleep()
        break
    default:
        print("Enter again")
        break
    }
    showMainMenu()
}


Command:

$ ./Library/Developer/Xcode/DerivedData/StandardInput-eauwzvcnsumxppbvwnagppvlylcg/Build/Products/Debug/StandardInput ; exit;


Result:

1) Walk
2) Study
3) Work
4) Sleep
> 1
Walking...
Enter 'exit' to finish
>
Walking...
Enter 'exit' to finish
>
Walking...
Enter 'exit' to finish
> exit
1) Walk
2) Study
3) Work
4) Sleep
> 2
Studying...
Enter 'exit' to finish
>
Studying...
Enter 'exit' to finish
>
Studying...
Enter 'exit' to finish
>
Studying...
Enter 'exit' to finish
> exit
1) Walk
2) Study
3) Work
4) Sleep
> 3
Working...
Enter 'exit' to finish
> exit
1) Walk
2) Study
3) Work
4) Sleep
> 4
Sleeping...
Enter 'exit' to finish
>
Sleeping...
Enter 'exit' to finish
>
Sleeping...
Enter 'exit' to finish
>
Sleeping...
Enter 'exit' to finish
>
Sleeping...
Enter 'exit' to finish
> exit
1) Walk
2) Study
3) Work
4) Sleep
> exit
Enter again
1) Walk
2) Study
3) Work
4) Sleep
> ^C
logout
Saving session...
...copying shared history...
...saving history...truncating history files...
...completed.

[Process completed]

Nintendo Switch: ARMS: ARMS Direct (2017.5.18) プレゼンテーション映像

Kosuke Yabuki (矢吹光佑)

Swift: Command Line Argument Parsing

Command:

$ cat ./Desktop/Arguments\ Test/Arguments\ Test/main.swift


Result:

import Foundation

var count = 0

for argument in CommandLine.arguments {
    print("Arg[\(count)]: \(argument)")
    count += 1
}


Command:

$ ./Library/Developer/Xcode/DerivedData/Arguments_Test-ffnafkbrsdftjxdawcsyrdhlfnvu/Build/Products/Debug/Arguments\ Test first second third


Result:

Arg[0]: ./Library/Developer/Xcode/DerivedData/Arguments_Test-ffnafkbrsdftjxdawcsyrdhlfnvu/Build/Products/Debug/Arguments Test
Arg[1]: first
Arg[2]: second
Arg[3]: third