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".
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:
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
}
}
}