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

26,122

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"
Share:
26,122
Bogdan Bogdanov
Author by

Bogdan Bogdanov

Junior Swift Programmer

Updated on October 25, 2020

Comments

  • Bogdan Bogdanov
    Bogdan Bogdanov over 3 years

    I have this code:

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

    How to add addInNToDelArray in NToDel:NSArray ?