Created
December 12, 2014 17:17
-
-
Save BradLarson/10fc463e8cb9afe5f927 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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