Skip to content

Instantly share code, notes, and snippets.

@stripe-q
Created July 13, 2016 12:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stripe-q/43bb92ab142454fd4728229bc0ffea4a to your computer and use it in GitHub Desktop.
Save stripe-q/43bb92ab142454fd4728229bc0ffea4a to your computer and use it in GitHub Desktop.
Array의 원소들을 개별 변수로 분해하는 함수.
enum DesctructError: ErrorProtocol {
case outOfIndex(String)
}
func desctruct<A> (array: [A]) throws -> (A, A) {
guard array.count > 1 else {
throw DesctructError.outOfIndex("the array doesn't have enough elements.")
}
return (array[0], array[1])
}
func desctruct<A> (array: [A]) throws -> (A, A, A) {
guard array.count > 2 else {
throw DesctructError.outOfIndex("the array doesn't have enough elements.")
}
return (array[0], array[1], array[2])
}
func desctruct<A> (array: [A]) throws -> (A, A, A, A) {
guard array.count > 2 else {
throw DesctructError.outOfIndex("the array doesn't have enough elements.")
}
return (array[0], array[1], array[2], array[3])
}
let str = "1 2 3 4"
let (x, y, z) = try! desctruct(array:str.characters.split(separator:" ").map{ Int(String($0))! })
print(x, y, z)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment