0

In my watchOS2 app I have array of tuples like this:

var medicines = [(String, String?, String?)]()

And in refreshing function i'd like to clear this array of tuples to append it with new String items. How can i do this ? I want to avoid having the same things in my array. Or maybe there is a better idea ?

My refresh function:

let iNeedCoreData = ["Value": "CoreData"]
    session.sendMessage(iNeedCoreData, replyHandler: { (content: [String: AnyObject]) -> Void in

        if let meds = content["reply"] as? [String: [String]] {

             self.medicines = [(String, String?, String?)]()


            if let medicineNames = meds["medicines"], amountNames = meds["amount"], timeNames = meds["time"] {
                if medicineNames.count != 0 {
                    self.addMedicines(medicineNames)
                    self.addQuantities(amountNames)
                    self.addTime(timeNames)
                    self.table.setHidden(false)
                    self.reloadTable()
                } else {
                    self.alertLabel.setHidden(false)
                }
            }
        }
        }) { (error) -> Void in
            print("We got an error from our watch device:" + error.domain)
    }

Adding to tuple funcs:

func reloadTable() {
    self.table.setNumberOfRows(medicines.count, withRowType: "tableRowController")
    var rowIndex = 0
    for item in medicines {
        if let row = self.table.rowControllerAtIndex(rowIndex) as? tableRowController {
            row.medicineLabel.setText(item.0)
            if let quantity = item.1, time = item.2 {
                row.amountLabel.setText(quantity)
                row.timeLabel.setText(time)

            }
            rowIndex++
        }
    }
}

func addMedicines(medicineNames: [String]) {
    for name in medicineNames {
        medicines.append((name, nil, nil))

    }
}

func addQuantities(quantities: [String]) {
    guard medicines.count == quantities.count else { return }
    for i in 0..<medicines.count {
        medicines[i].1 = quantities[i]
    }
}

func addTime(timeNames: [String]) {
    guard medicines.count == timeNames.count else { return }
    for i in 0..<medicines.count {

        medicines[i].2 = timeNames[i]
    }
}
kiuzio2
  • 11
  • 6

1 Answers1

1

Once the var has been declared, type hints are no longer needed.

self.medicines = []

I've tried to think of a few ways to overcome your problem here, but your code is very inflexible and needs to be refactored.

You are at the limit for the utility of tuples and need to turn medicine into a class or struct (use a struct) which supports Equatable.

In addition, you need to create an array of new objects, which can be merged into the existing self.medicines, building the new objects directly in self.medicines is very limiting.

Here is the tuple as a struct

struct Medicine: Equatable {
    let name: String
    let amount: String
    let time: String
}

func == (lhs: Medicine, rhs: Medicine) -> Bool {
    return lhs.name == rhs.name && lhs.amount == rhs.amount && lhs.time == rhs.time
}

Here is adding new values without removing old values or having duplicates

if let names = meds["medicines"], amounts = meds["amount"], times = meds["time"]
    where names.count == amounts.count && names.count == times.count
{
    for i in 0..<names.count {
        let medicine = Medicine(name: names[i], amount: amounts[i], time: times[i])

        if !medicines.contains(medicine) {
            medicines.append(medicine)
        }
    }
}
Jeffery Thomas
  • 42,202
  • 8
  • 92
  • 117
  • I want to avoid duplication those tuples in array, but making it empty and then refreshing doesn't work. Is there a better solution ? – kiuzio2 Nov 14 '15 at 12:16
  • @kiuzio2 How do you define equal tuples? When tuples are equal, should the old tuple be kept or should it be replaced by the new tuple? – Jeffery Thomas Nov 14 '15 at 14:00
  • I mean in my array of tuples i get something like this: `medicines = [("apap", "8 mg", "11:41") ...]` When i use button refresh to get new values old ones are duplicating. I want to avoid it. – kiuzio2 Nov 14 '15 at 14:26
  • @kiuzio2 So, is `("apap", "8 mg", "11:41")` different than `("apap", "8 mg", "11:42")`? – Jeffery Thomas Nov 14 '15 at 15:38
  • I don' really know what you mean, but yes. – kiuzio2 Nov 14 '15 at 15:43