我有错误(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不是成员。
答案 0 :(得分:2)
它是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
获取第一个元素。
答案 1 :(得分:0)
数组
let shoppingList = ["bananas", "milk", "vanilla ice cream"]
print("The most important item on my list is definitely \(shoppingList.last)")
NSArray
let shoppingList = ["bananas", "milk", "vanilla ice cream"] as NSArray
print("The most important item on my list is definitely \(shoppingList.lastObject)")
NSMutableArray
let shoppingList = ["bananas", "milk", "vanilla ice cream"] as NSMutableArray
print("The most important item on my list is definitely \(shoppingList.lastObject)")
答案 2 :(得分:0)