- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我有两个数组:
var addedToIgnoreList: [String] = []
var removedFromIgnoreList: [String] = []
我想将一个值附加到这些数组之一。如果我这样做:
(isUserIgnored ? removedFromIgnoreList : addedToIgnoreList).append("username")
我得到 Immutable value of type '[String]' only has mutating members named 'append'
如果我使用中间变量,它会起作用:
var which = isUserIgnored ? removedFromIgnoreList : addedToIgnoreList
which.append("username")
使用额外变量是唯一的方法吗?
更新:额外的变量将无法正常工作,因此 if
语句是唯一的选择。请参阅接受的答案以获取解释。
最佳答案
这都与数组是值类型有关,而不是引用类型。也就是说,变量不指向数组(不像 NSArray
)。它们是数组,将数组分配给新变量会生成一个新副本。
您收到该错误的原因是该语句:
(isUserIgnored ? removedFromIgnoreList : addedToIgnoreList).append(etc)
制作两个数组之一的临时副本,并且正在对该副本进行append
调用。该副本将是不可变的——这是一件好事,因为如果不是,你可能会无意中改变它(正如你在这里尝试的那样),结果却发现没有发生任何变化——你的副本被制作、变异,然后丢弃。
请记住:
var which = isUserIgnored ? removedFromIgnoreList : addedToIgnoreList
which.append("username")
也制作副本。所以改变 which
不会改变任何一个原始数组。
更改数组本身最直接的方法是使用 if
语句:
if isUserIgnored {
removedFromIgnoreList.append("username")
}
else {
addedToIgnoreList.append("username")
}
这不会复制,而是就地修改数组。
另一方面,如果您想要一个附加了值的新数组,最简单的方法可能是使用 +
运算符:
let newCopy = (isUserIgnored ? removedFromIgnoreList : addedToIgnoreList) + ["username"]
关于arrays - 选择要附加到三元运算符 'if' 运算符 : "Immutable value... only has mutating members" 的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29753014/
为什么我的简单对象不使用 Immutable.fromJS({}) 转换为 Immutable.Map() 这是 Map() - 按预期工作 > var mymap = Immutable.Map({
如何在Immutable.js列表的任意位置插入元素? 最佳答案 您正在寻找 splice method: Splice returns a new indexed Iterable by repla
https://facebook.github.io/immutable-js/docs/#/is 我尝试比较 2 个 OrderedMap,但 Immutable.is 函数不起作用。 它只能比较
对于标准的 JS 对象,可以使用解构赋值,例如: let obj = {name: 'james', code: '007'} let {name, code} = obj // creates ne
如何获取 immutable.js 映射的第一个键(不是值)? 基本上myMap.first()将返回值,但我对 key 感兴趣...... 我可以先执行 forEach 并存储,但必须是更好的方法!
查看this ,我认为一成不变。 Record是表示“javascript不可变对象(immutable对象)”的数据结构,但我想一次更新多个字段,而不是每次创建多个调用set的对象。 我想做这样的事
为什么可以改变 scala.collection.immutable.SortedMap 和 scala.collection.immutable.TreeMap? scala> import sca
好的,我有一个对象列表,非常标准。 const list = Immutable.List([{type:'thang',data:{id:'pants'}}]); 现在我想把裤子换成短裤……所以我在
如何在 OrderedMap 的末尾添加项目? 我试过了 this.boxes = this.boxes.setIn(data); 感谢您的帮助 最佳答案 一种可能的方法是使用 concat
在开始使用 reactjs 和 redux 进行开发之后,我认为在使用 redux 的同时使用 immutable.js 会更好。 但是...也许我是智障,或者在正确使用它之前需要一些练习,一切都崩溃
我要在 Immutable.js 映射上进行一系列更改。 我应该在什么时候更喜欢使用 withMutations而不是创建中间的不可变映射? 来自 Immutable.js 文档: If you ne
我想使用 Immutable 的流类型定义来描述 map 的形状。 您可以通过以下方式描述物体的形状: const stateShape: { id: number, isActive: bo
这两个 NuGet 包有什么区别?如果我正在创建 .net 4.51 项目,我应该使用其中一个吗?我过去使用过 Microsoft.Bcl.Immutable 的测试版,但只是想知道其中一个是否会比另
基本上我想要做的是,如果您按下按钮,那么条目应该获得一个新的 CEntry。如果有人可以帮助我,那就太好了。谢谢! struct AView: View { var entries = [CEn
我有来自框架的协议(protocol) public protocol BaseMappable { /// This function is where all variable mappi
Swift 开发相对较新。尝试修改其他人编写的几个函数。目标是在用户登录后第一次调用此路径(该部分尚未设置)时离开原始路径,并在后续调用中使用其他路径。 因此,在编写将指向首次通过或非首次通过的逻辑之
这段代码 extension Collection { mutating func f() { removeFirst() } } 处理错误 cannot use mutating m
我正在处理一些使用许多不同项目“列表”的 NodeJS 代码。底层实现使用包括 Immutable.List、Immutable.Set、Javascript Array 和 Javascript S
我正在尝试使用 Reacts Immutability helpers 或 Immutable js 合并一个不可变的对象数组。我正在尝试创建一个仅包含名称已更改的记录的列表。如果名称在提交前已多次更
我被困在一个 Cannot use mutating member on immutable value: function call returns immutable value 这段代码错误:
我是一名优秀的程序员,十分优秀!