gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-11-01 07:26:18 25 4
gpt4 key购买 nike

我有错误 (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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com