可变集合作为快速字典中的值被复制并且未被引用,因此变得不可变

时间:2018-07-04 12:36:12

标签: swift struct swift4 swift-dictionary

我的收藏集被复制到字典中,而不是存储为可变引用。

例如:

  var map = Dictionary<Int,Set<Int>>()
  var mySet = map[key, default: Set()]
  if mySet.count == 0 {
    // the collection doesn't exist in the dictionary so I am setting it
    // stored by value and not reference
    sectionToFeed[sectionId] = mapping
  }
  // this doesn't mutate the stored set
  mySet.insert(item)
  print(mySet)    <<<<<<<<<<<< Prints different collections
  print(map[key]) <<<<<<<<<<<<

0 个答案:

没有答案