Mohammed Nabil
Mohammed Nabil

Reputation: 157

Store value in Array Int in Swift

How to store the resultant print in an Array of Integer. I try to append the value in empty array but in the print it shows me empty. Below is the swift code till what i have done.

var num = [Int]()
            var filterMenId = ""
            if let indexPath = self.colEat.indexPathsForSelectedItems?.first{
                filterMenId = self.arrayMeal[indexPath.row].id.description
                let convert = Int(filterMenId) // Here i am getting the value, eg: 32
                print(num.append(convert!)) // Here i try to append but shows me empty

Upvotes: 0

Views: 416

Answers (1)

Sweeper
Sweeper

Reputation: 271355

append changes the array by adding an element to the end of the array only. It does not produce a value.

print takes a value and prints it. Well, append does not produce a value, so print(num.append(convert!)) prints (), which is a representation of "nothing"

More technically speaking, append returns Void, which is a typealias for an empty tuple (). The string representation of an empty tuple is, unsurprisingly, ().

If you want to print the array that would be produced if convert is added to num:

print(num + [convert!])

Note that this does not change num.

If you want to add convert to num, and then print num to show what elements it has after the change, you need two lines:

num.append(convert!)
print(num)

Upvotes: 1

Related Questions