Swift - Insert object/item / Add object/item in NSArray

Deadly 提交于 2019-12-21 12:27:09

问题


I have this code:

var NToDel:NSArray = []
var addInNToDelArray = "Test1 \ Test2"

How to add addInNToDelArray in NToDel:NSArray ?


回答1:


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"


来源:https://stackoverflow.com/questions/26551325/swift-insert-object-item-add-object-item-in-nsarray

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!