2016-11-05 4 views
0

私は数字をとり、その数字までのすべての偶数を文字列に表示しようとしています。現在、私はすべてがうまくいっていると思うが、ラベルに表示するのが難しい。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. 
} 
} 
+1

'getEvens(_ :)'は何も返しません... – Hamish

答えて

1

あなたgetEvens()機能は、コンソールに文字列を出力ではなくリターン何もしません。 (より正確には、その戻り型がVoid 又は()であり、それはあるtype '()'エラーメッセージで

「とタイプする 『)(』タイプの値を割り当てることはできません 『の文字列を?』

来ている。)

あなたはおそらくしたいことはあなたという

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 
} 

注意です

  • stride(from: 0, through: input, by: 2)inputから0から 偶数の配列を与え、
  • map(String.init)
  • 各番号を変換:もう少し「Swifty」であってもよい

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

    と同じ結果を達成することができます

  • joined(separator: ",")は結果を単一の 文字列に連結し、その間にコンマを追加しますements。
+0

私は "エディタのプレースホルダはソースファイルです"というエラーが表示される理由は何ですか? "let factor = getEvens(Input:Int)"私のコードで?私が助けてくれれば、あなたが提案した2番目の短いコードを使用しています。 – cisco21

+0

@ cisco21:実際の引数は 'let factor = getEvent(num)'です。 –

+0

スレッドを取得する1:テストするときに数字を入力するときはいつでも、ブレークポイント1.1。ポストをコードで更新しました。思考? – cisco21

関連する問題