如何访问这些字典值? (迅速)

时间:2016-03-17 13:59:45

标签: ios swift nsdictionary mindwave

我有一个名为eegsnapshot.swift的文件,它从EEG传感器读取值并将它们存储在字典中(我想?)。我无法尝试访问这些值。我是Swift的新手并且已经使用Arrays和Dictionaries做了一些工作,但是这里使用的方法比我以前遇到的方法更复杂。

有人能给我一些从字典中访问值的见解,例如“eegDelta”吗?

我希望使用这些值来控制SceneKit中的动画,但需要能够首先访问它们。

public struct EEGSnapshot {
    public let delta: Int
    public let theta: Int
    public let lowAlpha: Int
    public let highAlpha: Int
    public let lowBeta: Int
    public let highBeta: Int
    public let lowGamma: Int
    public let highGamma: Int

public static let allZeros = EEGSnapshot()

public init(dictionary: NSDictionary = [:]) {
    func extractPoint(key: String) -> Int {
        return dictionary[key] as? Int ?? 0
    }

    delta = extractPoint("eegDelta")
    theta = extractPoint("eegTheta")
    lowAlpha = extractPoint("eegLowAlpha")
    highAlpha = extractPoint("eegHighAlpha")
    lowBeta = extractPoint("eegLowBeta")
    highBeta = extractPoint("eegHighBeta")
    lowGamma = extractPoint("eegLowGamma")
    highGamma = extractPoint("eegHighGamma")

}


}



public extension NSDictionary {
    public convenience init(eegSnapshot: EEGSnapshot) {
    self.init(objects: [eegSnapshot.delta, eegSnapshot.theta,  eegSnapshot.lowAlpha, eegSnapshot.highAlpha, eegSnapshot.lowBeta, eegSnapshot.highBeta, eegSnapshot.lowGamma, eegSnapshot.highGamma],
        forKeys: ["eegDelta", "eegTheta", "eegLowAlpha", "eegHighAlpha", "eegLowBeta", "eegHighBeta", "eegLowGamma", "eegHighGamma"],
        count: 8)

       }

}

我试过

var deltaValue = EEGSnapshot["eegDelta"]

和类似但我收到错误“类型EEGSnapshot.Type没有下标成员”。

1 个答案:

答案 0 :(得分:0)

你可以做Dictionary<> EEGSnapshot转换如下:

public struct EEGSnapshot {

    public let delta: Int
    public let theta: Int
    public let lowAlpha: Int
    public let highAlpha: Int
    public let lowBeta: Int
    public let highBeta: Int
    public let lowGamma: Int
    public let highGamma: Int

    public static let allZeros = EEGSnapshot()

    public init(dictionary: [String : Int] = [:]) {

        self.delta = dictionary["eegDelta"] ?? 0
        self.theta = dictionary["eegTheta"] ?? 0
        self.lowAlpha = dictionary["eegLowAlpha"] ?? 0
        self.highAlpha = dictionary["eegHighAlpha"] ?? 0
        self.lowBeta = dictionary["eegLowBeta"] ?? 0
        self.highBeta = dictionary["eegHighBeta"] ?? 0
        self.lowGamma = dictionary["eegLowGamma"] ?? 0
        self.highGamma = dictionary["eegHighGamma"] ?? 0
    }

    public var dictionary: [String : Int] {

        return [
            "eegDelta" : self.delta,
            "eegTheta" : self.theta,
            "eegLowAlpha" : self.lowAlpha,
            "eegHighAlpha" : self.highAlpha,
            "eegLowBeta" : self.lowBeta,
            "eegHighBeta" : self.highBeta,
            "eegLowGamma" : self.lowGamma,
            "eegHighGamma" : self.highGamma
        ]
    }
}

如果您想下载像快照[" key"]这样的EEGSnapshots,那么您必须添加下标支持:

Swift Subscripts

extension EEGSnapshot {

    public subscript(key: String) -> Int {

        switch key {

            case "eegDelta":
                return self.delta

            case "eegTheta":
                return self.theta

            case "eegLowAlpha":
                return self.lowAlpha

            case "eegHighAlpha":
                return self.highAlpha

            case "eegLowBeta":
                return self.lowBeta

            case "eegHighBeta":
                return self.highBeta

            case "eegLowGamma":
                return self.lowGamma

            case "eegHighGamma":
                return self.highGamma

            default:
                return 0
        }
    }
}