Skip to content

Swift type that can read a numbers from the standard input stream in various ways.

Notifications You must be signed in to change notification settings

asistech-org/Console

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 

Repository files navigation

Console

One more way to get a number data from the standard input stream.

Example 1:

let console = Console()

let a = console.readInt()

print(a, console.report.message, console.report.error, "\n")

let b = console.readDouble()

print(b, console.report.message, console.report.error, "\n")

let c = console.readDouble()

print(c, console.report.message, console.report.error, "\n")

Console:

10
Optional(10) The input data is correct. NoError 

1.2
Optional(1.2) The input data is correct. NoError 

qwe
nil The input data is not correct. IncorrectData

Example 2:

let console = Console()

var a = console.nextInt()
var b = console.nextInt()
var c = console.nextInt()

print(a, b, c)
print(console.report.message, console.report.error, "\n")

Console:

10 12 45
Optional(10) Optional(12) Optional(45)
The input data is correct. NoError

Example 3:

let console = Console()

var a = console.nextInt()
var b = console.nextInt()
var c = console.nextInt()

print(a, b, c)
print(console.report.message, console.report.error, "\n")

a = console.nextInt()
b = console.nextInt()

print(a, b)
print(console.report.message, console.report.error, "\n")

c = console.nextInt()

print(c)
print(console.report.message, console.report.error, "\n")

Console:

2 7 3 5 9 8 1 4
Optional(2) Optional(7) Optional(3)
The input data is correct. NoError 

Optional(5) Optional(9)
The input data is correct. NoError 

Optional(8)
The input data is correct. NoError 

Example 4:

let console = Console()

var a = console.nextInt()
var b = console.nextInt()

print(a, b)
print(console.report.message, console.report.error, "\n")

console.deleteInputSequence()

a = console.nextInt()
b = console.nextInt()

print(a, b)
print(console.report.message, console.report.error, "\n")

Console:

2 5 3 7 9
Optional(2) Optional(5)
The input data is correct. NoError 

7 4 3 1 8
Optional(7) Optional(4)
The input data is correct. NoError

Example 5:

let console = Console()

var a = console.readDoubleArray()

print(a)
print(console.report.message, console.report.error, "\n")

Console:

2.5 3 qwe 4.8
[Optional(2.5), Optional(3.0), nil, Optional(4.8)]
The input data is not correct. IncorrectData 

Example 6:

let console = Console()

guard var a = console.readInt() else {
    throw console.report.error
}

print(a)

Console:

10
10

Console:

qwe
Fatal error: Error raised at top level: Console.Console.Report.IncorrectData: file Swift/ErrorType.swift, line 200
2020-09-05 08:35:41.957398 Console[6063:133538] Fatal error: Error raised at top level: Console.Console.Report.IncorrectData: file Swift/ErrorType.swift, line 200

Example 7:

let console = Console()

if let a = console.readInt() {
    print(a + 1)
} else {
    print(console.report.message)
}

Console:

11
12

Console:

qwe
The input data is not correct.

Console:

The input data is empty.

About

Swift type that can read a numbers from the standard input stream in various ways.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages