I have some code now that essentially has three for loops that I'd like to shrink. I tried to write a small function to do it, but stuck on how to pass an enum value that normally takes an associated value as just a 'type', so I can associate the value in the routine.
I fully realize that I can probably create some other ErrorType subclass, or perhaps use a enum that has a tuple associated with it - I'm sure there are a vast number of ways to solve my problem (and I'll probably just use one shortly).
However, curiousity prompts me to post this, to see if there is in fact some way to modify my function parameter to fullfill the intention:
private enum RecommendationErrors: ErrorType {
case StringError(String)
case VarsError(String)
case ImageError(String)
}
func parseRecommendations(item: [String: AnyObject]) throws -> [String: String] {
var dict = [String: String]()
var stringItems: [String]
func process(d: [String: AnyObject], err: RecommendationErrors) {
for s in stringItems {
if let val = d[s] as? String {
dict[s] = val
} else {
throw err(s) <<<--- compilee error
}
}
}
...
How about the following code? You can refer to the associated type enum via a closure. For example, you can pass: RecommendationErrors.StringError
private enum RecommendationErrors: ErrorType {
case StringError(String)
case VarsError(String)
case ImageError(String)
}
func parseRecommendations(item: [String: AnyObject]) throws -> [String: String] {
var dict = [String: String]()
var stringItems: [String]
func process(d: [String: AnyObject], err: (String)->RecommendationErrors) throws {
for s in stringItems {
if let val = d[s] as? String {
dict[s] = val
} else {
throw err(s)
}
}
}
...