问题描述
我有以下代码:
var NToDel:NSArray = []
var addInNToDelArray = "Test1 \ Test2"
如何添加 addInNToDelArray
code> NToDel:NSArray ?
How to add addInNToDelArray
in NToDel:NSArray
?
推荐答案
您不能- NSArray
是一个不可变的数组,因此一旦实例化就无法更改。
应该将其转换为 NSMutableArray
,在这种情况下,它很简单:
You can't - NSArray
is an immutable array, and as such once instantiated it cannot be changed.You should turn it into a NSMutableArray
, and in that case it's as simple as:
NToDel.addObject(addInNToDelArray)
或者,您可以插入值在实例化时间:
Alternatively, you can insert the value at instantiation time:
var NToDel:NSMutableArray = [addInNToDelArray]
但这不是要添加,而是要初始化-实际上,在那行之后,您不能对数组元素进行任何更改。
but that's not about adding, it's about initializing - and in fact, after that line you cannot do any change to the array elements.
请注意,您的字符串中有一个错误: \
字符必须按如下所示转义:
Note that there's an error in your string: the \
character must be escaped as follows:
var addInNToDelArray = "Test1 \\ Test2"
这篇关于Swift-在NSArray中插入对象/项目/添加对象/项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!