Swift - Insert object / element / Add object / element to NSArray

I have this code:

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

How to add addInNToDelArray to NToDel:NSArray ?

+6
source share
1 answer

You cannot - NSArray is an immutable array, and as such, after its creation, it cannot be modified. You have to turn it into an NSMutableArray , in which case it will be simple:

 NToDel.addObject(addInNToDelArray) 

Alternatively, you can insert a value during instance creation:

 var NToDel:NSMutableArray = [addInNToDelArray] 

but this is not about adding, but about initialization - and in fact, after this line you cannot change the elements of the array.

Please note that there is an error in your line: the \ character must be escaped as follows:

 var addInNToDelArray = "Test1 \\ Test2" 
+17
source

Source: https://habr.com/ru/post/977191/


All Articles