Skip to content

Instantly share code, notes, and snippets.

@BradLarson
Created December 12, 2014 17:17
Show Gist options
  • Select an option

  • Save BradLarson/10fc463e8cb9afe5f927 to your computer and use it in GitHub Desktop.

Select an option

Save BradLarson/10fc463e8cb9afe5f927 to your computer and use it in GitHub Desktop.
import Foundation
class AssociatedValues {
enum Stuff {
case One(String)
case Two(String)
case Three(String)
}
func whichOneAreWe(currentAssociatedValue:Stuff) -> String {
switch currentAssociatedValue {
case let .One(japaneseValue):
return japaneseValue
case let .Two(spanishValue):
return spanishValue
case let .Three(germanValue):
return germanValue
}
}
// if we have a non-optional case, this compiles properly
// func whichTwoAreWe(valueList:(firstPlainValue:Stuff, secondPlainValue:Stuff)...) -> String {
// var outputString = ""
//
// for (possibleFirstValue, possibleSecondValue) in valueList {
// outputString += self.whichOneAreWe(possibleFirstValue)
//
// outputString += self.whichOneAreWe(possibleSecondValue)
//
// }
//
// return outputString
// }
// When we pass in an optional case, this crashes the compiler
func whichTwoAreWe(valueList:(firstPlainValue:Stuff?, secondPlainValue:Stuff?)...) -> String {
var outputString = ""
for (possibleFirstValue, possibleSecondValue) in valueList {
if let firstValue = possibleFirstValue {
outputString += self.whichOneAreWe(firstValue)
}
if let secondValue = possibleSecondValue {
outputString += self.whichOneAreWe(secondValue)
}
}
return outputString
}
}
let myInstance = AssociatedValues()
myInstance.whichTwoAreWe((.One("ichi"), .Two("dos")), (.Two("dos"), .Three("drei")))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment