我在操场上制作的这段代码代表了我的问题:
import Foundation var countries = ["Poland":["Warsaw":"foo"],"England":["London":"foo"]] for (country,city) in countries { if city["London"] != nil { city["London"] = "Piccadilly Circus" // error because by default the variables [country and city] are constants (let) } }
有没有人知道一项工作或最好的方法来使这项工作?
您可以通过在其声明中添加var来使city变为可变:
for (country,var city) in countries {
不幸的是,更改它不会影响您的国家/地区词典,因为您将获得每个子词典的副本.要做你想做的事,你需要遍历国家的钥匙并从那里改变一切:
for country in countries.keys { if countries[country]!["London"] != nil { countries[country]!["London"]! = "Picadilly Circus" } }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。