0

I have a table that is populated with the following data:

var vaccineEntry: NSMutableArray = [[
    ["name" : "Rabies 1-yr", "detail": "Set"],
    ["name" : "Rabies 3-yr", "detail": "Set"],
    ["name" : "Distemper", "detail": "Set"],
    ["name" : "Parvovirus", "detail": "Set"],
    ["name" : "Adenovirus", "detail": "Set"]],
[
    ["name" : "Parainfluenza", "detail": "Set"],
    ["name" : "Bordetella", "detail": "Set"],
    ["name" : "Lyme Disease", "detail": "Set"],
    ["name" : "Leptospirosis", "detail": "Set"],
    ["name" : "Canine Influenza", "detail": "Set"]
]]

var section = ["Core Dog Vaccines", "Non-Core Dog Vaccines"]

My tableview methods are working since whatever I put in these array of dictionary places populates my table correctly. However, my app will be updating all of the “detail” values depending on a boolean which then translates to a string. I can’t seem to find the right NSMutable Array method to perform this translation though. Here is my code for that:

if object["expired"] as! Bool == true {

        let expiredTag: String = "Expired"

        self.vaccineEntry.setValue("Expired", forKey: "Rabies 1-yr")
        self.vaccineEntry.setValue(expiredTag, forKey: (name: "Rabies 1-yr"))
        self.vaccineEntry.setValue(expiredTag, forKeyPath: "Rabies 1-yr")
        self.vaccineEntry.valueForKeyPath("Rabies 1-yr")
        self.vaccineEntry.setValue("Expired", forKeyPath: "Rabies 1-yr")
        self.vaccineEntry.objectAtIndex(0).valueForKeyPath("name")
        self.vaccineEntry.setValue("Expired", forKey: "name")
        self.vaccineEntry.objectAtIndex(0).valueForKey("Rabies 1-yr")
        self.vaccineEntry.replaceObjectAtIndex(0, withObject: "Expired")
        let rabiesObject = ["name" : "Rabies 1-yr", "detail": "Expired"]
        self.vaccineEntry.replaceObjectAtIndex(0, withObject: rabiesObject)


 } else {

        let updatedTag: String = "Up To Date"

        self.vaccineEntry.setValue("UP to date", forKey: "name")
        self.vaccineEntry.objectAtIndex(0).valueForKey("Rabies 1-yr")

 }

These are all of my attempts. They all compile fine but my table data does not change from my original input at the top (“Set” is just placeholder text). I had each of these attempts commented out one by one as I was making my attempts, fyi. Any help is much appreciated!!

2
  • 2
    I strongly recommend to use a custom class rather than a dictionary for example class Vaccine { var name = "" var detail = "" var expired = false } . That makes it so much easier to update values. Commented Nov 18, 2015 at 19:31
  • @vadian thanks for taking the time to look at this. This is my first app, can you please explain how this will benefit in the long run? Or possibly present a sample model to help me understand? Thanks! Commented Nov 18, 2015 at 20:23

2 Answers 2

1
import Foundation

var vaccineEntry: NSMutableArray = [[
    ["name" : "Rabies 1-yr", "detail": "Set"],
    ["name" : "Rabies 3-yr", "detail": "Set"],
    ["name" : "Distemper", "detail": "Set"],
    ["name" : "Parvovirus", "detail": "Set"],
    ["name" : "Adenovirus", "detail": "Set"]],
    [
        ["name" : "Parainfluenza", "detail": "Set"],
        ["name" : "Bordetella", "detail": "Set"],
        ["name" : "Lyme Disease", "detail": "Set"],
        ["name" : "Leptospirosis", "detail": "Set"],
        ["name" : "Canine Influenza", "detail": "Set"]
    ]]

print(vaccineEntry[0][0].dynamicType)
//vaccineEntry[0][0] = nil  // error: cannot assign to immutable expression of type 'AnyObject!'

var vaccineEntry2: Array<Array<Dictionary<String,String>>> = [[
    ["name" : "Rabies 1-yr", "detail": "Set"],
    ["name" : "Rabies 3-yr", "detail": "Set"],
    ["name" : "Distemper", "detail": "Set"],
    ["name" : "Parvovirus", "detail": "Set"],
    ["name" : "Adenovirus", "detail": "Set"]],
    [
        ["name" : "Parainfluenza", "detail": "Set"],
        ["name" : "Bordetella", "detail": "Set"],
        ["name" : "Lyme Disease", "detail": "Set"],
        ["name" : "Leptospirosis", "detail": "Set"],
        ["name" : "Canine Influenza", "detail": "Set"]
    ]]
vaccineEntry2[0][0]["name"] = "EDIT"
print(vaccineEntry2)
/*

[[["detail": "Set", "name": "EDIT"], ["detail": "Set", "name": "Rabies 3-yr"], ["detail": "Set", "name": "Distemper"], ["detail": "Set", "name": "Parvovirus"], ["detail": "Set", "name": "Adenovirus"]], [["detail": "Set", "name": "Parainfluenza"], ["detail": "Set", "name": "Bordetella"], ["detail": "Set", "name": "Lyme Disease"], ["detail": "Set", "name": "Leptospirosis"], ["detail": "Set", "name": "Canine Influenza"]]]

*/
Sign up to request clarification or add additional context in comments.

1 Comment

I see what you did here. Thank you so much!
0

You could model your data into a more "structured" way. This would make things much easier.

You'll need a few things.

Types...

enum State { case Set, Edit }

enum Type: String, CustomStringConvertible {
    case
        Core = "Core Dog Vaccines",
        NonCore = "Non-Core Dog Vaccines"
    var description: String { return self.rawValue }
}

struct Vaccine: CustomStringConvertible {
    let name: String
    var detail: State
    let type: Type

    var description: String { return "name: \(name), detail: \(detail), type: \(type)" }
}

...a couple of lists of vaccines...

var coreVaccines: [Vaccine] = [
    Vaccine(name: "Rabies 1-yr", detail: .Set, type: .Core),
    Vaccine(name: "Rabies 3-yr", detail: .Set, type: .Core),
    Vaccine(name: "Distemper", detail: .Set, type: .Core),
    Vaccine(name: "Parvovirus", detail: .Set, type: .Core),
    Vaccine(name: "Adenovirus", detail: .Set, type: .Core),
]

var nonCoreVaccines: [Vaccine] = [
    Vaccine(name: "Parainfluenza", detail: .Set, type: .NonCore),
    Vaccine(name: "Bordetella", detail: .Set, type: .NonCore),
    Vaccine(name: "Lyme Disease", detail: .Set, type: .NonCore),
    Vaccine(name: "Leptospirosis", detail: .Set, type: .NonCore),
    Vaccine(name: "Canine Influenza", detail: .Set, type: .NonCore)
]

...and a function

func change(name: String, newState: State, inout list:[Vaccine]) {
    guard let index = (list.indexOf { $0.name == name }) else {
        debugPrint("Error: could not find a vaccine with name \(name)")
        return
    }
    list[index].detail = newState
}

Test

coreVaccines[2] // > name: Distemper, detail: Set, type: Core Dog Vaccines
change("Distemper", newState: .Edit, list: &coreVaccines)
coreVaccines[2] // > name: Distemper, detail: Edit, type: Core Dog Vaccines

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.