简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM