简体   繁体   中英

Need help updating NSMutable Array - iOS Swift

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!!

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"]]]

*/

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM