0

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
5
  • That correct. Array.append returns void, which is then printed. You may want to call append before the print statement and then just print(num) or print (convert!), depending on your needs Commented Mar 3, 2021 at 8:21
  • 1
    Surely it printed (), didn't it? Commented Mar 3, 2021 at 8:25
  • @AndreasOetjen I want to print value after append Commented Mar 3, 2021 at 8:28
  • @Sweeper i want to see value not () Commented Mar 3, 2021 at 8:29
  • I know you want to see the value, but saying "it shows empty" is misleading here, as () is definitely not empty. Commented Mar 3, 2021 at 8:30

1 Answer 1

1

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)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.