Swift_集合 let element = arr.first

2020-12-16  本文已影响0人  Eyes_cc
/// The first element of the collection.
///
/// If the collection is empty, the value of this property is `nil`.
///
/// - Complexity: O(1)
    @inlinable public var first: Element? { get }

let numbers = [10, 20, 30, 40, 50]
if let firstNumber = numbers.first {
    print(firstNumber)
}
// Log: 10
/// The last element of the collection.
///
/// If the collection is empty, the value of this property is `nil`.
///
/// - Complexity: O(1)
    @inlinable public var last: Element? { get }

let numbers = [10, 20, 30, 40, 50]
if let lastNumber = numbers.last {
    print(lastNumber) 
}
// Log: 50
上一篇下一篇

猜你喜欢

热点阅读