【问题标题】:Need help updating NSMutable Array - iOS Swift需要帮助更新 NSMutable 数组 - iOS Swift
【发布时间】:2016-02-20 16:32:42
【问题描述】:

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

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”只是占位符文本)。在我进行尝试时,我将这些尝试中的每一个都一一注释掉,仅供参考。非常感谢任何帮助!

【问题讨论】:

  • 我强烈建议使用自定义类而不是字典,例如 class Vaccine { var name = "" var detail = "" var expired = false } 。这使得更新值变得更加容易。
  • @vadian 感谢您抽出宝贵时间查看此内容。这是我的第一个应用程序,您能解释一下从长远来看这将如何受益吗?或者可能提供一个示例模型来帮助我理解?谢谢!

标签: ios swift


【解决方案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"]]]

*/

【讨论】:

  • 我明白你在这里做了什么。非常感谢!
【解决方案2】:

您可以将数据建模为更“结构化”的方式。这会让事情变得更容易。

你需要一些东西。

类型...

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-25
    • 2013-12-06
    • 2021-01-22
    相关资源
    最近更新 更多