ios - Swift 中的数组不计算对象

标签 ios arrays string swift int

想知道为什么这不起作用?

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


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

self.counterLabel.text = array.capacity

我收到以下错误,我想知道有人知道如何解决这个问题吗?我正在尝试让文本标签显示数组中的对象数量。

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

最佳答案

您正在使用 Int 类型的值设置 String 类型的值,因此需要将 Int 转换为 String:

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

正如评论者所提到的,您正在寻找的特性很重要

关于ios - Swift 中的数组不计算对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34359760/

相关文章:

ios - 蓝牙:如何让 ios 尝试 "App Not Installed"

ios - Apple mach-o 链接器错误 - MoPubSDK

iphone - 我可以使用 switch 语句的哪些替代方法来更新我的 UITableViewCells?

javascript - ES6 数组过滤器删除常见项

javascript - 使用下划线根据属性值过滤对象数组,无论大小写?

php - 将 .php 数组传递到 javascript 代码 jquery/ajax

Java:带有 unicode 字符的 String.toCharArray()

ios - 具有动态单元格内容的 UICollectionView - 让单元格记住状态

c# - string.Join 抛出 OutOfMemory 异常

c++ - std::vector<const char *> 的赋值效果错误?