swift - 访问 For in 循环 Collection View Swift

标签 swift

我创建了这个数组: let yourShares = ["90%","40%", "23%","15%","10%"] 然后访问这个:

 for yourShare in yourShares {
        dataSource[0].yourShareTitle = "90%"
        dataSource[1].yourShareTitle = "40%"
        dataSource[2].yourShareTitle = "23%"
        dataSource[3].yourShareTitle = "10%"
    }

然后为了访问它,我将此代码放入 Collection View 的扩展中

让 yourShare = model.dataSource?[indexPath.row] cell.yourShareLabel.text = "您的分享\(yourShare!.yourShareTitle)" cell.titleLabel?.font = UIFont.systemFont(ofSize: 1)

问题是当我运行它时它只是打印可选。为什么,我该如何解决这个问题? 〜谢谢

最佳答案

你需要打开它

if let yourShare = model.dataSource?[indexPath.row]?.yourShareTitle as? String {
        cell.yourShareLabel.text = "Your Share \(yourShare)"
        cell.titleLabel?.font = UIFont.systemFont(ofSize: 1)
}

关于swift - 访问 For in 循环 Collection View Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45203555/

相关文章:

swift - 如何在swift中为UI页面控制点仅设置layer.border-color

ios - 当旋转或放大/缩小 map 时,自定义 MKAnnotationView 位置会发生巨大变化

iOS - [WKWebView .cxx_construct] 只能在主线程中使用

swift - 映射结构数组

ios - 异常 : 'NSInternalInconsistencyException' , 原因 : 'Could not load NIB in bundle: ' NSBundle. .... 在 Swift 4 中

swift - 我在哪里获取 nameOfPromisedFilesDroppedAtDestination 的 NSURL

swift - Collection View 单元格按钮未触发操作

ios - Swift 循环遍历具有多维数组的 Dictionary plist

ios - 使用 UITableView 的老虎机动画

ios - 深层链接到健康应用程序的医疗 ID 选项卡?