2016-05-29 2 views
0

私はウェイトリフティングの時間をコードしようとしています。繰り返しに4つのフェーズがあります。スウィフト浮動小数点値と "X"で型を作る:Polloquin演習記法

エキセントリック(時間は、重量を下げる過ごした) ボトム(時間は、リフトの下で過ごした) は 同心(時間が重量を持ち上げる費やした)トップ(時間のトップを過ごしましたリフト)

次のようにフォーマットされますので、一例では、人はその後すぐに、3秒を取って重量を持ち上げ、移動の最後に到達して、重量を下げる1秒かかるだろう1030

1つの繰り返しを完了するために停止します。

class rep { 

    var eccentric:Float // time spent lowering the weight 
    var bottom:Float // time spent at the bottom of the repetition. 
    var concentric:Float // time spent raising the weight. 
    var top:Float // time spent at the top of the repetition. 

    var notation:String 

    init(timeDown:Float, timeBottom:Float, timeUp:Float, timeTop:Float)  { 

    eccentric = timeDown 
    bottom = timeBottom 
    concentric = timeUp 
    top = timeTop 

    notation = "\(eccentric),\(bottom),\(concentric),\(top)" 

} 

func displayNotation() -> String{ 

    print(notation) 

    return notation 

    } 

} 





class ViewController: UIViewController { 

override func viewDidLoad() { 
    super.viewDidLoad() 


    let repetition = rep(timeDown: 1,timeBottom: 0,timeUp: 3,timeTop: 0) 

    repetition.displayNotation() 



} 

これは私が何をしたいのか1.0,0.0,3.0,0.0

を出力指示するための追加の文字「X」がある「できるだけ速く」を私はこれのために新しいタイプを作る必要があると思っていますか?だから私は浮動小数点またはその特定の文字を受け入れることができるようにしたい...これについて行く方法については完全に困惑している。

任意の応答[OK]を

+0

「追加の文字X」とまったく同じ意味ですか?どのような場合にこの文字を使用するのでしょうか? – xoudini

+0

ユーザー入力 - ユーザーは、4段階のリフトのそれぞれについて、0.0から9.9までのフロート値または「X」を選択できます。これは、私が0.0から9.9の値とxを設定する必要があるピッカーのビュー要素で行われます。 また、アプリケーション内の他の場所にデータを表示し、入力されたデータを使用して計算を行います。 文字列を使うことはできますが、これは "X"を選択しなかった場合に文字列からユーザー入力をFloatに変換することを意味し、これを行うにはそれほどぎこちない方法があると思った – user1923938

+0

あなたのニーズを満たす。 – xoudini

答えて

0

のおかげで、これはそれについて移動する一つの方法です。

まず、あなたのデータのためのモデルを作成します。

class Data { 
    var string: String 
    var value: Double? 

    init(string: String, value: Double?) { 
     self.string = string 
     self.value = value 
    } 
} 

stringを表示するために使用され、valueは計算に使用されます。私はvalueをオプションとして設定しましたが、これについてはすぐに説明します。


はその後UIPickerViewのデータソースを作成し、それを移入:

var dataSource: [Data] = [] 

// Adds all values from 0.0 to 9.9 and the "additional character". 
func populateDataSource() { 
    for i in 0..<100 { 
     let value = Double(i)/10 
     dataSource.append(Data(string: value.description, value: value)) 
    } 
    dataSource.append(Data(string: "X", value: nil)) 
} 

私はここでやったことはnilへの追加文字のvalueを設定されています。

func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int { 
    return 1 
} 

func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int { 
    return dataSource.count 
} 

func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? { 
    return dataSource[row].string 
} 

// This variable will be used to hold the user selection. 
var selected: Data? 

// If you want it to default to e.g. 0.0, just create it as: 
// var selected = dataSource.first 

func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) { 
    self.selected = dataSource[row] 
} 

をそして今、あなたが選択に基づいて計算を行うことができます:

// You should check that selection isn't nil before doing this. 
// Depends on how you create it. 

if let value = selection.value { 
    // The selection has a value between 0.0 and 9.9. 
    // So, do your standard calculations for that. 
} else { 
    // The selection does not have a value, which means it's your custom character. 
    // So, take that into account in your calculations. 
} 
+0

うわあ、ありがとう。私が望んでいたよりもはるかに深い。私はクラスを使っていると思っていましたが、何とかenumを使うことができると思っていました。私は浮動小数点数の代わりに整数を決定しましたが、これは多かれ少なかれ使用される方法です: – user1923938

0


あなたUIPickerViewUIPickerViewDataSourceメソッドを追加するには、すでに設定したと仮定すると、どのように私は以下のソリューションを使用して終了しました。 CharacterSetsやEnumでこれを行う方法があるはずですが、それは進んでいきましょう。

import UIKit 

class Data { 

    var string: String 
    var value: Int? 

    init(string:String, value:Int?){ 

     self.string = string 
     self.value = value 

    } 

} 


var dataSource: [Data] = [] 

func populateDataSource(){ 

    for i in 0..<10{ 

     dataSource.append( Data(string:i.description, value:i) ) 

    } 

    dataSource.append( Data(string:"X", value: nil) ) 

} 

class Notation { 

    var concentric: Data 
    var bottom: Data 
    var eccentric: Data 
    var top: Data 

    var display: String 

    init(down:Data,bottom:Data,up:Data,top:Data){ 

     self.concentric = down 
     self.bottom = bottom 
     self.eccentric = up 
     self.top = top 

     display = "\(self.concentric.string)/\(self.bottom.string)/\(self.eccentric.string)/\(self.top.string)" 

    } 
} 


class Exercise{ 
    var sets, reps, rest, weight:Int 
    var tempo:Notation 

    init(sets:Int,reps:Int,rest:Int,weight:Int?,tempo:Notation){ 

     self.sets = sets 
     self.reps = reps 
     self.rest = rest 
     self.weight = weight! 
     self.tempo = tempo 

    } 

} 

populateDataSource() 


var maxStrengthTempo = Notation(
    down: dataSource[3], // 1 
    bottom: dataSource[1], // 1 
    up: dataSource[10],  // X 
    top: dataSource[0]  // 0 
) 

var deadlift = Exercise(sets: 3, reps: 5, rest: 60, weight:200, tempo: maxStrengthTempo) 


print(deadlift.tempo.display) 

// console: 3/1/X/0 
関連する問題