现在在底部带有一个简短的最小可行示例
我看到了这种怪异的行为..我试图将时间序列条目的位置设置为true:
struct TimeSeriesEntry: Codable, Equatable {
let id: String
let uid: String
let date: Date
let apps: [String:Double]
let locations: [String:Bool]
func changeApp(app: String, value: Double) -> TimeSeriesEntry {
var apps = self.apps
apps[app] = value
return TimeSeriesEntry(id: self.id, uid: self.uid, date: self.date, apps: apps, locations: self.locations)
}
}
此代码没有扩展名。完全是TimeSeriesEntry
。
我正在调试器中对此进行评估,因为出于某种原因始终将其设置为 false ,但是我在代码中将其硬编码为 true 。它在调试器中的工作原理相同:
TimeSeriesEntry(id: String(describing: UUID()), uid: DBIFirebase.uid,
date: data.date.round(to: TimeSeries.defaults.interval,
TimeSeries.defaults.intervalComponent), apps:[:], locations:
[data.id:true])
.locations["c72b2cc2-897e-4621-8fba-452cf43afa0a"]
... = (Bool?) false
在其他地方,当我分配apps
时,双打效果很好。我对为什么发生这种情况有些困惑。
实际分配是在此函数中进行的,在代码的此分支中,它是函数中位置的部分,而不是应用程序中的部分:
func timeSeriesData (appData: AppData? = nil, locationData: LocationData? = nil) -> TimeSeriesEntry? {
if let data = appData {
return TimeSeriesEntry(id: String(describing: UUID()), uid: DBIFirebase.uid, date: data.date.round(to: TimeSeries.defaults.interval, TimeSeries.defaults.intervalComponent), apps: [data.id:data.metric], locations:[:])
} else {
guard let data = locationData else {
return nil
}
return TimeSeriesEntry(id: String(describing: UUID()), uid: DBIFirebase.uid, date: data.date.round(to: TimeSeries.defaults.interval, TimeSeries.defaults.intervalComponent), apps:[:], locations: [data.id:true])
}
}
使其成为完整的独立mve:
import Foundation
struct TimeSeriesEntry: Codable, Equatable {
let id: String
let uid: String
let date: Date
let apps: [String:Double]
let locations: [String:Bool]
func changeApp(app: String, value: Double) -> TimeSeriesEntry {
var apps = self.apps
apps[app] = value
return TimeSeriesEntry(id: self.id, uid: self.uid, date: self.date, apps: apps, locations: self.locations)
}
}
struct LocationData: Codable, Equatable {
// let name: String
let id: String
let date: Date
let latitude: Double
let longitude: Double
}
func timeSeriesData (locationData: LocationData) -> TimeSeriesEntry? {
return TimeSeriesEntry(id: String(describing: UUID()), uid: "DBIFirebase.uid", date: locationData.date, apps:[:], locations: [locationData.id:true])
}
let df = DateFormatter()
df.dateFormat = "yyyy-MM-dd HH:mm:ssZ"
let locationData = LocationData(
id: "c72b2cc2-897e-4621-8fba-452cf43afa0a",
date: df.date(from: "2017-08-28 00:00:01Z")!,
latitude: 67.1404,
longitude: -151.4804)
let timeSeriesEntry = timeSeriesData(locationData: locationData)
在repl中,位置将包含1个具有正确ID的条目,但bool设置为 false :
39. let timeSeriesEntry = timeSeriesData(locationData: locationData)
df: DateFormatter = {
Foundation.Formatter = {
baseNSObject@0 = {
isa = NSDateFormatter
}
}
}
locationData: LocationData = {
id = "c72b2cc2-897e-4621-8fba-452cf43afa0a"
date = 2017-08-28 00:00:01 UTC
latitude = 67.1404
longitude = -151.4804
}
timeSeriesEntry: TimeSeriesEntry? = some {
id = "C836C671-B266-4CA1-9A80-841E3329C6C8"
uid = "DBIFirebase.uid"
date = 2017-08-28 00:00:01 UTC
apps = 0 key/value pairs
locations = 1 key/value pair {
[0] = {
key = "c72b2cc2-897e-4621-8fba-452cf43afa0a"
value = false
}
}
}
编辑:好的,现在我很困惑。.我在repl中玩了一点,我明白了:
42> timeSeriesEntry!.locations["c72b2cc2-897e-4621-8fba-452cf43afa0a"]
$R1: Bool? = true
43> timeSeriesEntry!
$R2: TimeSeriesEntry = {
id = "C836C671-B266-4CA1-9A80-841E3329C6C8"
uid = "DBIFirebase.uid"
date = 2017-08-28 00:00:01 UTC
apps = 0 key/value pairs
locations = 1 key/value pair {
[0] = {
key = "c72b2cc2-897e-4621-8fba-452cf43afa0a"
value = false
}
}
}
44> timeSeriesEntry!.locations
$R3: [String : Bool] = 1 key/value pair {
[0] = {
key = "c72b2cc2-897e-4621-8fba-452cf43afa0a"
value = false
}
}