2

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?'

1 Answer 1

3

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

Sign up to request clarification or add additional context in comments.

3 Comments

this won't solve the problem because he is accessing the wrong property. He should be using String(array.count)
I was using count but tried out capacity but I got a value of 4 with only 3 objects in the array. But that solution did work for me, I just replaced capacity with count.
Can also do "\(array.count)"

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.