• 0
Votes
name
name Punditsdkoslkdosdkoskdo

How to get the key's value and finish my delegate in ios

I'm a beginner so I apologize if this all looks very hacky.

I have a dictionary of Times and Time Amounts In Seconds. This is a countdown timer app with a pickerView that lets you pick the countdown time start and once you pick a time in didSelect, it updates the timerLabel with that time amount. Then when you tap play, it plays down from that time amount.

I am trying to use a protocol in pickerView's didSelect to tell me when a time has been selected, then match the time to its value of Time amount in seconds, convert Seconds to Hours:Minutes:Seconds and pass that info to the timer so that it can play down from that time. However, i'm getting stuck with finishing the delegate.

I'm getting this error: Cannot subscript a value of type '[String : Int]' with an index of type '[String]' when I try to call for the value of the key in the protocol stub. I understand that basically I have an array and I can't get the key's value from it but I need help figuring out how to go backwards now... from array back to keys and values. I put all of Times in an array for pickerView but I'm getting stuck with how to get the value for the key selected in the array of Times I gave to pickerview.

Here is my code so far with where the error shows:

import UIKit

class TimerVC: UIViewController, TimerValueInSecondsDelegate {

// Times and Time Amounts In Seconds
let selectedTimes:[(timeName: String, timeAmount: Double)] = [("3 MIN", 180), ("5 MIN", 300), ("10 MIN", 600), ("15 MIN", 900)]

var delegate: TimerValueInSecondsDelegate?
var counter = 1 //Starting value of seconds
var timer = Timer()
var isTimerRunning = false //Make sure only one timer is created at a time.
let customWidth: CGFloat = 120
let customHeight: CGFloat = 60

@IBOutlet weak var timePickerView: UIPickerView!
@IBOutlet weak var startTimerButton: UIButton!
@IBOutlet weak var timerNumbers: UILabel! //Countdown label
@IBOutlet weak var cancelTimer: UIButton! //Cancel the timer

override func viewDidLoad() {
    super.viewDidLoad()

    timePickerView.dataSource = self
    timePickerView.delegate = self

    cancelTimer.isHidden = true
    timerNumbers.isHidden = true

    startTimerButton.setImage(UIImage(named : "startTimerButton"), for: UIControlState.normal)
    startTimerButton.setImage(UIImage(named : "pauseTimerButton"), for: UIControlState.selected)
    startTimerButton.addTarget(self, action: #selector(startTimerAction), for: UIControlEvents.touchUpInside)
    self.view.addSubview(startTimerButton)
}

You have to get the item from the array with the row index

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
    let time = timePickerArray[row]
    delegate?.getSecondsValue(from: time)
}

and declare getSecondsValue

func getSecondsValue(from time : String) {
  let timeInSeconds = selectedTimes[time] 
}

Edit:

Rather than a tuple use a struct

struct Time {
   let name : String
   let amount : Double
}

...

let selectedTimes = [Time(name:"3 MIN", amount:180.0), 
                     Time(name:"5 MIN", amount:300.0), 
                     Time(name:"10 MIN", amount:600.0), 
                     Time(name:"15 MIN", amount:900.0)]

...

func getSecondsValue(from time : Time) {
   let timeAmountValue = time.amount
}

...

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
    let time = selectedTimes[row]
    delegate?.getSecondsValue(from: time)
}

...

pickerLabel.text = selectedTimes[row].name
  • 1
Reply Report