Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I have this code:

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

How to add addInNToDelArray in NToDel:NSArray ?

like image 993
Bogdan Bogdanov Avatar asked Oct 24 '14 15:10

Bogdan Bogdanov


Video Answer


1 Answers

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"
like image 65
Antonio Avatar answered Nov 02 '22 19:11

Antonio