我在没有太多帮助的情况下看过几个类似的SO问题,
我正在尝试使用availabilityTimes
timesArray
更新周一updateDailyTimes
availabilityTimes
和timesArray
var availabilityTimes = [
"Monday": [ "available" : true,
"times": [
["startTime": "9:00 am", "endTime": "1:30 pm" ],
["startTime": "2:30 pm", "endTime": "6:00 pm" ],
["startTime": "7:30 pm", "endTime": "9:00 pm" ]
]
],
"Tuesday": [ "available" : true,
"times": [
["startTime": "9:00 am", "endTime": "6:00 pm" ]
]
]
]
var timesArray = [
["startTime": "9:00 am", "endTime": "1:30 pm" ],
["startTime": "2:30 pm", "endTime": "6:00 pm" ],
["startTime": "7:30 pm", "endTime": "9:00 pm" ]
]
这是我的代码,当我在下面调用函数时,我在代码行cannot assign to immutable expression of type [[String : String]]
value["times"] as [[String : String]] = timesArray
func updateDailyTimes(day: String, timesArray: [[String:String]]){
guard var updatedTimes = self.availabilityTimes as? [String: AnyObject] else{
return
}
for (key, var value) in updatedTimes {
if key == day{
for (key1, value1) in value as! [String : AnyObject]{
if key1 as String == "times" {
value["times"] as [[String : String]] = timesArray // I get an error message in this line
}
}
}
}
}
答案 0 :(得分:2)
根据我的评论,我的意思是开始和结束时间的自定义结构Times
和具有所需属性的结构DailyTimes
和更新Times
数组的变异函数: / p>
struct Times {
let start : String
let end : String
}
struct DailyTimes {
let weekday : String
var available : Bool
var times = [Times]()
mutating func update(times : [Times]) {
self.times = times
}
}
现在创建availableTimes
字典:
var availableTimes = ["Monday" : DailyTimes(weekday: "Monday", available: true, times: [Times(start: "9:00 am", end:"1:30 pm"),
Times(start: "2:30 pm", end:"6:00 pm"),
Times(start: "7:30 pm", end:"9:00 pm")]),
"Tuesday" : DailyTimes(weekday: "Tuesday", available: true, times: [Times(start: "9:00 am", end:"6:00 pm")])]
和timesArray
数组:
let timesArray = [Times(start: "9:00 am", end:"1:30 pm"),
Times(start: "2:30 pm", end:"6:00 pm"),
Times(start: "7:30 pm", end:"9:00 pm")]
由于Times
的{{1}}数组已经包含Monday
值{更新timesArray
Tuesday
最后证明
availableTimes["Tuesday"]?.update(times:timesArray)
另一项改进可能是使用print(availableTimes["Tuesday"]?.times)
代替DateComponents
s。
答案 1 :(得分:1)
在Swift中,Dictionary
是一个结构,它是一个值类型。可以将值类型视为基本类型,例如int
。
如果你有这个:
int a = 0
int b = a
然后b得到a的值,它们不相同。
因此,当您循环遍历字典时,您的(键,值)对实际上并不是字典中的对,它们只具有相同的值。在迭代它时,不要改变值或引用也很重要。这可能导致奇怪的错误。
enum AvailabilityTimeMarker {
case start
case end
}
enum DayOfTheWeek: String {
case monday = "Monday"
case tuesday = "Tuesday"
case wednesday = "Wednesday"
case thursday = "Thursday"
case friday = "Friday"
case saturday = "Saturday"
case sunday = "Sunday"
}
typealias Availability = [DayOfTheWeek : AvailabilityDay]
typealias AvailabilityDay = [String : Any]
typealias AvailabilityArray = [AvailabilityTime]
typealias AvailabilityTime = [AvailabilityTimeMarker : String]
var availabilityTimes: Availability = [
.monday : [ "available" : true,
"times": [
["startTime": "9:00 am", "endTime": "1:30 pm" ],
["startTime": "2:30 pm", "endTime": "6:00 pm" ],
["startTime": "7:30 pm", "endTime": "9:00 pm" ]
]
],
.tuesday : [ "available" : true,
"times": [
["startTime": "9:00 am", "endTime": "6:00 pm" ]
]
]
]
var timesArray: AvailabilityArray = [
[.start : "9:00 am", .end : "1:30 pm" ],
[.start : "2:30 pm", .end : "6:00 pm" ],
[.start : "7:30 pm", .end : "9:00 pm" ]
]
func updateDailyTimes(day: DayOfTheWeek, timesArray: AvailabilityArray) {
let availabilities = self.availabilityTimes
var newtimes = availabilities
if var newDayAvailability = availabilities[day] {
newDayAvailability["times"] = newtimes
newtimes[day] = newDayAvailability
self.availabilityTimes = newtimes// Since we're not using reference semantics, we have to change the entire value
}
}