需要帮助更新NSMutable Array - iOS Swift

时间:2015-11-18 19:20:58

标签: ios swift

我有一个填充了以下数据的表:

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

我的tableview方法正在运行,因为无论我放在这些字典数组中,我都能正确地填充表格。但是,我的应用程序将根据布尔值更新所有“细节”值,然后转换为字符串。我似乎无法找到正确的NSMutable Array方法来执行此转换。这是我的代码:

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

 }

这些都是我的尝试。它们都编译得很好,但我的表数据不会从顶部的原始输入改变(“Set”只是占位符文本)。我正在尝试这些尝试中的每一个,因为我正在尝试,fyi。非常感谢任何帮助!!

2 个答案:

答案 0 :(得分: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"]]]

*/

答案 1 :(得分:0)

您可以将数据建模为更多&#34;结构化的&#34;办法。这会让事情变得更容易。

你需要做一些事情。

类型...

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)" }
}

......几个疫苗清单......

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

...和一个函数

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
}

测试

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