Trenton Tyler
Trenton Tyler

Reputation: 83

Array not counting objects in Swift

Curious as to why this is not working?

@IBOutlet weak var counterLabel: UILabel! <-- Outlet/var


let array : [Int] = [1,2,3]

self.counterLabel.text = array.capacity

I am getting the error below and I was wondering anybody knew how to fix this? I am trying to get the text label to display the number of objects in the array.

Cannot assign a value of type 'Int' to a value of type 'String?'

Upvotes: 2

Views: 269

Answers (1)

Jordan Davies
Jordan Davies

Reputation: 10861

You're setting a value of type String with a value of type Int so you need to convert the Int to a String:

self.counterLabel.text = String(array.count)

As commenters have mentioned, the property you're looking for is count

Upvotes: 3

Related Questions