0

I have a file called eegsnapshot.swift which reads values from an EEG Sensor and stores them in a dictionary (I think?). I am having trouble trying to access these values. I am new to Swift and have done some work with Arrays and Dictionaries but the method used here is more complicated than what I have come across before.

Can anyone give me some insight into accessing a value from the dictionary, for example "eegDelta"?

I am hoping to use these values to control animations in SceneKit but need to be able to access them first.

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)

       }

}

I've tried

var deltaValue = EEGSnapshot["eegDelta"]

and similar but I get the error "Type EEGSnapshot.Type has no subscript members".

ChloeS
  • 3
  • 3
  • What do you mean "trouble trying to access these values"? Are you getting them or not? Do you have any errors? – Adrian Krupa Mar 17 '16 at 14:03
  • I don't know what code to use to get the values. I've tried `var deltaValue = EEGSnapshot["eegDelta"]` and similar but I get the error "Type EEGSnapshot.Type has no subscript members". – ChloeS Mar 17 '16 at 14:08
  • try `EEGSnapshot()["eegDelta"]` to avoid this error. But I am not sure that I get the question. To get a value from any dictionary, you should use `dictionary["key"]` – Faruk Mar 17 '16 at 14:13
  • Thanks, I've tried this but still get the same error "EEGSnapshot has no subscript members" – ChloeS Mar 17 '16 at 14:17
  • 1
    instantiate EEGSnapshot using the init that you wrote, then get it using eegSnapshot.delta. Or, if you already have an EEGSnapshot and want to turn it back into a dictionary (although you already have a dictionary you used to create the EEGSnapshot), then you can use that dictionary convenience init that you created – Will M. Mar 17 '16 at 14:20
  • Try `var deltaValue = EEGSnapshot().delta` – MwcsMac Mar 17 '16 at 14:36
  • Thanks for the replies, @MwcsMac's method worked – ChloeS Mar 17 '16 at 14:46

1 Answers1

0

You can do Dictionary <> EEGSnapshot conversions like this:

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

If you want to subscript EEGSnapshots like snapshot["key"] then you have to add the subscript support:

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
        }
    }
}
m.ylmz
  • 61
  • 4