简体   繁体   English

如何将readLine()的Swift 3输出转换为Integer?

[英]How to convert Swift 3 output of readLine() to Integer?

Please don't mark as duplicate until you read the whole thing. 在你阅读整篇文章之前,请不要标记为重复。 This is specific to Swift 3. 这是Swift 3特有的。

I have functions that have parameters such as Ints, Floats, etc. I'd like to take the output of readLine() and have Swift accept the output of readLine() as these types, but unfortunately readLine() outputs a String? 我有函数有Ints,Floats等参数。我想取readLine()的输出并让Swift接受readLine()的输出作为这些类型,但不幸的是readLine()输出一个String? and when I try to convert it tells me it's not unwrapped. 当我尝试转换时它告诉我它没有被打开。 I need help. 我需要帮助。 I'm using Ubuntu 16.04. 我正在使用Ubuntu 16.04。

For example, if I had area(width: 15, height: 15) , how would I replace 15 and 15 with two constants containing readLine() or any equivalent to readLine() to accept input from a user in the terminal? 例如,如果我有area(width: 15, height: 15) ,我如何用包含readLine()的两个常量或者readLine()的任何等价来替换15和15来接受来自终端用户的输入?

Also note that the program I am writing is specifically doing math, as most people seem to be happy with strings, this is literally a CLI-based calculator. 另请注意,我写的程序是专门做数学的,因为大多数人似乎对字符串感到满意,这实际上是一个基于CLI的计算器。

EDIT 1 (lol) Okay, here's a more exact explanation of above. 编辑1(lol)好的,这里有一个更准确的解释。 The following code will print the area of a trapezoid: 以下代码将打印梯形区域:

import Foundation

func areaTrapezoid(height: Float, baseOne: Float, baseTwo: Float) {
    let inside = baseOne + baseTwo
    let outside = 0.5 * height
      let result = outside * inside
        print("Area of Trapezoid is \(result)")
  }

areaTrapezoid(height: 10, baseOne: 2, baseTwo: 3)

So, the trapezoid has a height of 10 units, and two bases that have lengths of 2 and 3 respectively. 因此,梯形的高度为10个单位,两个基部的长度分别为2和3。 However, I want to do something like: 但是,我想做的事情如下:

import Foundation

func areaTrapezoid(height: Float, baseOne: Float, baseTwo: Float) {
    let inside = baseOne + baseTwo
    let outside = 0.5 * height
      let result = outside * inside
        print("Area of Trapezoid is \(result)")
  }

let h = readLine()
areaTrapezoid(height: h, baseOne: 2, baseTwo: 3)

Except, as is already obvious, readLine() will output an optional string, and not a Float. 除了,很明显,readLine()将输出一个可选字符串,而不是Float。 I want the user to be able to input the numbers via CLI in sort of an interactive way, if you will. 我希望用户能够以交互方式通过CLI输入数字,如果愿意的话。 I'm just learning Swift, but I did something similar in C++ when I was learning that language. 我只是学习Swift,但是当我学习那门语言时,我在C ++中做了类似的事情。 Thanks for any help you can provide. 感谢您的任何帮助,您可以提供。

readLine() returns an Optional String. readLine()返回一个Optional String。

To unwrap the String, you can use if let , and to convert the String to an integer, use Int() . 要解包String,可以使用if let ,并将String转换为整数,使用Int()

Example: 例:

import Foundation
if let typed = readLine() {
  if let num = Int(typed) {
      print(num)
  }
}

Let's say you prompted the user twice: 假设您提示用户两次:

let prompt1 = readLine()
let prompt2 = readLine()

Then: 然后:

if let response1 = prompt1, 
    response2 = prompt2, 
    num1 = Int(response1), 
    num2 = Int(response2) {
    print("The sum of \(num1) and \(num2) is \(num1 + num2)")
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM