-3

I have a [String] called tableData. I want to save data to that array whenever I get new data from another view controller.
How can I do that?

var NewPlaylistName: String?
var NewImageCover: String?

var tableData = ["Evo X", "458", "GTR", "Evo X", "458", "GTR", "Evo X", "458", "GTR", "Evo X", "458", "GTR"]

override func viewDidLoad() {
    super.viewDidLoad()

    if NewPlaylistName != nil {
        tableData += ["\(NewPlaylistName!)"]
        tableImages += ["evox.jpg"]

        var newTableData = tableData
        println(newTableData)
    }
}

If I run the code above I always get:

[Evo X, 458, GTR, Evo X, 458, GTR, Evo X, 458, GTR, Evo X, 458, GTR, sDfsad]

...the same total of array even though I already renew the data.
What I want to know is, how can I add more data to the array?

For example:

[Evo X, 458, GTR, Evo X, 458, GTR, Evo X, 458, GTR, Evo X, 458, GTR, sDfsad, afasds]

...and then add more when there is new data:

[Evo X, 458, GTR, Evo X, 458, GTR, Evo X, 458, GTR, Evo X, 458, GTR, sDfsad, afasds, kljkl]

...and so on.

Marcus Rossel
  • 3,196
  • 1
  • 26
  • 41
Ega Setya Putra
  • 1,645
  • 6
  • 23
  • 51

3 Answers3

1

To add an element to the end of the array, use the .append() method or the += operator. These two statements do the same:

myArray.append(23)
myArray += 29

The array [1,2,3,5,7,11,13,17] appends two numbers, changing to: [1,2,3,5,7,11,13,17,23,29] The advantage to the += operator is you can append another array if the elements are of the same type in both arrays.

myArray += [31, 37]

results in [1,2,3,5,7,11,13,17,23,29,31,37] In Swift, operators keep a consistency across type, which might make some traditional programmers a bit squeamish. The += in their mind is an arithmetic operator. You should not use it for arrays, only for incrementing numbers, like we did in the for loop. Swift continues such consistency with merging two arrays with the + operator

var myStringArray = ["apple","pear","banana","raspberry"]
var myOtherStringArray = ["Pepperoni","Sausage"]
var jointArray = myStringArray + myOtherStringArray

This code results in jointArray being ["apple","pear","banana","raspberry","Pepperoni","Sausage"]

To insert an element at a particular position, use the .insert(at:index) method, remembering that the first index is zero:

myArray.insert(2,atindex:2)

This results in [1,2,2,3,5,7,11,13,17,23,29,31,37]

To change an element, assign a value to the specific element, like this:

var myArray = ["apple","pear","banana","raspberry"]
myArray[1] = "blackberry"

Will change myArray to ["apple","blackberry","banana","raspberry"]. You can change a range of values if both arrays are the same type.

var myArray = ["apple","pear","banana","raspberry"]
myArray[1...2] = ["beagle bone","arduino"]

Will change ["apple","blackberry","banana","raspberry"] to ["apple","beagle bone","arduino","raspberry"] . Range values specify the range to replace. You can replace with fewer items

myArray[1..3] = ["blackBerry", "kit kat"]

Changes elements 1,2,3 to only two elements making the array ["apple","beagle bone","arduino","raspberry"] to ["apple","blackBerry", "kit kat"]

Reference from : array-in-swift

This might helps you :)

Yuyutsu
  • 2,509
  • 22
  • 38
0

I will give you the reference.Just follow that

  STEP 1:If you want to  append more elements  to your array, you can  use the += operator, or the extend function.

   tableData += ["\(NewPlaylistName!)"]
   tableData.extend(["\(NewPlaylistName!)"])

If you want to need some detail explanation,just follow the below reference.You will get answer.

Add an element to an array in Swift

Community
  • 1
  • 1
user3182143
  • 9,459
  • 3
  • 32
  • 39
-1

You can define any array Like this.

var myArr= [];

then you can add whatever data (any DataType or Object) you want to this array at any point of execution like this

myArr.push(anyDataEntry); 

or you can access the array like this

for (i in myArr) 
{
    //myArr[i]
}
Bannings
  • 10,376
  • 7
  • 44
  • 54
Kamal Pundir
  • 287
  • 4
  • 13