私は数字をとり、その数字までのすべての偶数を文字列に表示しようとしています。現在、私はすべてがうまくいっていると思うが、ラベルに表示するのが難しい。print evens app error help - タイプ文字列に変換できません
エラーは次のとおりです。label.text = factor
エラーは次のように表示されます。 "'(文字列型'() 'の値を' string 'に割り当てることはできません)
私は誰もがこの問題を解決するか、私のコードを改善し、私が間違っていたかを説明するためにどのように任意のアイデアを持っていた場合、私はまだ種類の新しいスウィフトによ不思議だった
ここに私のコードです:?。
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をブレークポイントを設定して数int型を入力してくださいoアプリケーションをテストするときのテキストボックス。なぜこれが起こっているのだろうか?
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.
}
}
'getEvens(_ :)'は何も返しません... – Hamish