在Swift中,有没有办法将数组元素分配给多个变量?
In Swift, is there a way to to assign elements of array to multiple variables?
我有一个像这样的长数组:
let array = [1, 2, 3]
我想像这样将所有元素的值分配给不同的变量:
var a, b, c:Int
[a, b, c] = array
我知道用元组是可能的,但我需要用数组来做,而不是单独做每个变量。
在可用的 list of patterns 中,没有“数组模式”或类似的东西,因此您不能对数组进行模式匹配。但是,您可以做的是:
let array = [1,2,3]
let (a, b, c) = (array[0], array[1], array[2])
如果数组少于 3 个元素,这将在运行时抛出错误。
它很有用,但不是内置的。非可变泛型让它变得很痛苦。
let (a, b, c) = [1, 2, 3].tuple3!
/// A workaround for not being able to extend tuples.
public struct Tuple<Elements> {
public var elements: Elements
public init(_ elements: Elements) {
self.elements = elements
}
}
public extension Tuple {
// MARK: - 2-tuple
/// Create a new tuple with one more element.
static subscript<Element0, Element1, Element2>(
tuple: Elements, element: Element2
) -> (Element0, Element1, Element2)
where Elements == (Element0, Element1) {
(tuple.0, tuple.1, element)
}
// MARK: - 3-tuple
/// Create a new tuple with one more element.
static subscript<Element0, Element1, Element2, Element3>(
tuple: Elements, element: Element3
) -> (Element0, Element1, Element2, Element3)
where Elements == (Element0, Element1, Element2) {
(tuple.0, tuple.1, tuple.2, element)
}
}
public extension Sequence {
typealias Tuple2 = (Element, Element)
typealias Tuple3 = (Element, Element, Element)
typealias Tuple4 = (Element, Element, Element, Element)
var tuple2: Tuple2? { makeTuple2()?.tuple }
var tuple3: Tuple3? { makeTuple3()?.tuple }
var tuple4: Tuple4? { makeTuple4()?.tuple }
private func makeTuple2() -> (
tuple: Tuple2,
getNext: () -> Element?
)? {
var iterator = makeIterator()
let getNext = { iterator.next() }
guard
let _0 = getNext(),
let _1 = getNext()
else { return nil }
return ((_0, _1), getNext)
}
private func makeTuple3() -> (
tuple: Tuple3,
getNext: () -> Element?
)? {
guard
let (tuple, getNext) = makeTuple2(),
let element = getNext()
else { return nil }
return (Tuple[tuple, element], getNext)
}
private func makeTuple4() -> (
tuple: Tuple4,
getNext: () -> Element?
)? {
guard
let (tuple, getNext) = makeTuple3(),
let element = getNext()
else { return nil }
return (Tuple[tuple, element], getNext)
}
}
我有一个像这样的长数组:
let array = [1, 2, 3]
我想像这样将所有元素的值分配给不同的变量:
var a, b, c:Int
[a, b, c] = array
我知道用元组是可能的,但我需要用数组来做,而不是单独做每个变量。
在可用的 list of patterns 中,没有“数组模式”或类似的东西,因此您不能对数组进行模式匹配。但是,您可以做的是:
let array = [1,2,3]
let (a, b, c) = (array[0], array[1], array[2])
如果数组少于 3 个元素,这将在运行时抛出错误。
它很有用,但不是内置的。非可变泛型让它变得很痛苦。
let (a, b, c) = [1, 2, 3].tuple3!
/// A workaround for not being able to extend tuples.
public struct Tuple<Elements> {
public var elements: Elements
public init(_ elements: Elements) {
self.elements = elements
}
}
public extension Tuple {
// MARK: - 2-tuple
/// Create a new tuple with one more element.
static subscript<Element0, Element1, Element2>(
tuple: Elements, element: Element2
) -> (Element0, Element1, Element2)
where Elements == (Element0, Element1) {
(tuple.0, tuple.1, element)
}
// MARK: - 3-tuple
/// Create a new tuple with one more element.
static subscript<Element0, Element1, Element2, Element3>(
tuple: Elements, element: Element3
) -> (Element0, Element1, Element2, Element3)
where Elements == (Element0, Element1, Element2) {
(tuple.0, tuple.1, tuple.2, element)
}
}
public extension Sequence {
typealias Tuple2 = (Element, Element)
typealias Tuple3 = (Element, Element, Element)
typealias Tuple4 = (Element, Element, Element, Element)
var tuple2: Tuple2? { makeTuple2()?.tuple }
var tuple3: Tuple3? { makeTuple3()?.tuple }
var tuple4: Tuple4? { makeTuple4()?.tuple }
private func makeTuple2() -> (
tuple: Tuple2,
getNext: () -> Element?
)? {
var iterator = makeIterator()
let getNext = { iterator.next() }
guard
let _0 = getNext(),
let _1 = getNext()
else { return nil }
return ((_0, _1), getNext)
}
private func makeTuple3() -> (
tuple: Tuple3,
getNext: () -> Element?
)? {
guard
let (tuple, getNext) = makeTuple2(),
let element = getNext()
else { return nil }
return (Tuple[tuple, element], getNext)
}
private func makeTuple4() -> (
tuple: Tuple4,
getNext: () -> Element?
)? {
guard
let (tuple, getNext) = makeTuple3(),
let element = getNext()
else { return nil }
return (Tuple[tuple, element], getNext)
}
}