consider:
enum line { case horizontal(cgfloat) case vertical(cgfloat) } let leftedge = line.horizontal(0.0) let leftmaskrightedge = line.horizontal(0.05) how can access, say, lefedge's associated value, directly, without using switch statement?
let noideahowto = leftedge.associatedvalue + 0.5 this doesn't compile!
i had @ these so questions none of answers seem address issue.
the noideahowto non compiling line above should one-liner, because associated value can type, fail see how user code write "generic" or associatedvalue method in le enum itself.
i ended this, gross, , needs me revisit code each time add/modify case ...
enum line { case horizontal(cgfloat) case vertical(cgfloat) var associatedvalue: cgfloat { { switch self { case .horizontal(let value): return value case .vertical(let value): return value } } } } any pointer anyone?
as others have pointed out, kind of possible in swift 2:
import coregraphics enum line { case horizontal(cgfloat) case vertical(cgfloat) } let min = line.horizontal(0.0) let mid = line.horizontal(0.5) let max = line.horizontal(1.0) func dotoline(line: line) -> cgfloat? { if case .horizontal(let value) = line { return value } return .none } dotoline(min) // prints 0 dotoline(mid) // prints 0.5 dotoline(max) // prints 1
Comments
Post a Comment