打印均匀应用程序错误帮助 - 无法转换为类型字符串

时间:2016-11-05 21:38:17

标签: swift string text label

我一直在尝试取一个数字并在字符串中显示所有偶数以及所述数字。目前,我认为我的一切正常,但我无法将其显示在标签上。

我得到的错误是:label.text = factor。 错误说明:"无法分配类型的值'()'输入' string?'

我很想知道是否有人知道如何解决这个问题或改进我的代码并解释我做错了什么?我对Swift来说还是个新手。

这是我的代码:

import UIKit

class ViewController: UIViewController {

@IBOutlet var input1 : UITextField!
@IBOutlet var label : UILabel!

@IBAction func factorAction(sender: UIButton) {
    if let text = input1.text {
        if let num = Int(text) {
            // text to int
            let factor = getEvens(<#T##input: Int##Int#>)
            label.text = factor
        } else {
            // Show the user that the entered text isn't a number
        }
    } else {
        // There's no text
    }
}
// notifies user when no text or if a non number string/whole integer has been entered.
// recycled code from factor application because it was useful 

func getEvens(_ input: Int){
    var output: String = ""
    for i in 0 ... input {
        if (i % 2 == 0){
            output += String(i) + ","
        }
    }
    output.remove(at: output.index(before: output.endIndex))
    print(output);
}    // returns result

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

}

再次感谢你们!

编辑2:我继续更新代码;目前,我正在尝试在测试应用程序时在文本框中输入一个数字,从而得到一个1:断点1.1。有什么想法会发生这种情况吗?

import UIKit

class ViewController: UIViewController {
@IBOutlet weak var input1 : UITextField!
@IBOutlet weak var label : UILabel!

@IBAction func factorAction(sender: UIButton) {
    if let text = input1.text {
        if let num = Int(text) {
            // text to int
            let factor = getEvens(num)
            label.text = factor
        } else {
            // Show the user that the entered text isn't a number
        }
    } else {
        // There's no text
    }
}

// notifies user when no text or if a non number string/whole integer has been entered.
// recycled code from factor application because it was useful

func getEvens(_ input: Int) -> String {
    let output = stride(from: 0, through: input, by: 2)
        .map(String.init)
        .joined(separator: ",")
    return output
}

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
}
}

1 个答案:

答案 0 :(得分:1)

您的getEvens()函数会将一个字符串输出到控制台,但不会返回任何内容。 (更准确地说,它的返回类型是Void(),这是错误消息中的type '()'

  

“无法指定类型'()'的值来键入'string?'

来自。)

你可能想要的是

func getEvens(_ input: Int) -> String { // <-- Return type is `String`
    var output: String = ""
    for i in 0 ... input {
        if (i % 2 == 0){
            output += String(i) + ","
        }
    }
    output.remove(at: output.index(before: output.endIndex))
    return output // <-- The returned value
}

请注意,您可以使用

获得相同的结果
func getEvens(_ input: Int) -> String {
    let output = stride(from: 0, through: input, by: 2)
        .map(String.init)
        .joined(separator: ",")
    return output
}

可能更“Swifty”:

  • stride(from: 0, through: input, by: 2)给出一系列的 从0input的偶数,
  • map(String.init)将每个数字转换为字符串,然后
  • joined(separator: ",")将结果连接到一个 字符串,在元素之间添加逗号。