ios - 类型 '[String]' 的值没有成员 'lastObject'

标签 ios xcode swift

我有错误 (shoppingList.lastObject)

    var shoppingList = ["bananas", "milk", "vanilla ice cream"]
    var itemCount = shoppingList.count
    print("There are \(itemCount) items.")
    for item in shoppingList
    {
        print("I need to buy \(item).")
    }
    print("The most important item on my list is definitely \(shoppingList.lastObject)")

它说 lastObject 不是成员。

最佳答案

它是last 而不是lastObject:

var shoppingList = ["bananas", "milk", "vanilla ice cream"]
var itemCount = shoppingList.count
print("There are \(itemCount) items.")
for item in shoppingList {
    print("I need to buy \(item).")
}
print("The most important item on my list is definitely \(shoppingList.last)")

注意:使用.first获取第一个元素。

关于ios - 类型 '[String]' 的值没有成员 'lastObject',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35909824/

相关文章:

具有多个 imageView 输出的 iOS 相机

swift - 我的快速方法没有输出

ios - 如何在 ARKit 和 SceneKit 中正确缩放 DAE 模型?

ios - 在 icloud 上禁用文档文件夹及其所有子文件夹的备份?

iphone - 如果我将我的应用程序部署目标设置为 5.1,它是否与 iOS 7.0 版本兼容

ios - 如何从 Xcode 运行在 iPhone 上保留应用程序?

swift - 如何显示 CloudKit 中最接近今天日期的帖子?

ios - 为 Collection View 舍入浮点值时出错

ios - 删除带有 CoreData 和 NSFetchedResultsController 的 UITableView 行

Ruby gem Xcodeproj 在项目保存时抛出异常