Saturday, May 27, 2017
Friday, May 26, 2017
Thursday, May 25, 2017
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]
$ 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]
ラベル:
Standard streams,
Swift,
標準ストリーム
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
$ 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
Subscribe to:
Posts (Atom)