Thanks Erica. I’ve been able to transform arrays of strings into arrays of my OptionSets using an enum approach like you describe. I was looking more for a generic approach that I could apply to all of the various OptionSets I have to decode from JSON. I suppose whether it’s from an array of strings or array of the OptionSet is less important, but getting to the array of the OptionSet itself is something I can already do.
Thanks, Jon > On Nov 3, 2016, at 9:37 PM, Erica Sadun <er...@ericasadun.com> wrote: > > Maybe something like this? Or you could just bitwise || individual sets. Or > you could use a dictionary to lookup [string: rawValue]. etc. > > public struct MyOptionSet: OptionSet { > public static let one = MyOptionSet(rawValue: 1 << 0) > public static let two = MyOptionSet(rawValue: 1 << 1) > public static let three = MyOptionSet(rawValue: 1 << 2) > > public var rawValue: Int { return _rawValue } > public init(rawValue: Int) { self._rawValue = rawValue } > private let _rawValue: Int > > private enum StringEnum: String { case one, two, three } > public init(strings: [String]) { > var set = MyOptionSet() > strings.flatMap({ StringEnum(rawValue: $0) }) > .flatMap({ MyOptionSet(rawValue: 1 << $0.hashValue) }) > .forEach { set.insert($0) } > _rawValue = set.rawValue > } > } > > let stringArray: [String] = ["one", "three"] > let stringOptions = MyOptionSet(strings: stringArray) > stringOptions.rawValue > >> On Nov 3, 2016, at 7:09 PM, Jon Shier via swift-users <swift-users@swift.org >> <mailto:swift-users@swift.org>> wrote: >> >> Swifters: >> I’m dealing with a JSON API where sets of options are returned as >> arrays of strings. Representing these as OptionSets seems ideal. I can >> decode the arrays of strings into an array of individual OptionSet values, >> but I’ve run into a dead end when trying generically reduce the array of >> OptionSets to a single OptionSet value. I’ve tried variety of ways of >> definition a Collection extension, even tried defining a global function, >> but I can’t seem to use the OptionSet sequence initializer or reduce itself >> (cannot invoke insert with argument of type (OptionSet) (or T)). Any >> guidance here? >> Here’s what I’ve tried: >> >> extension Collection where Iterator.Element == OptionSet { >> >> func reduced() -> Iterator.Element { >> return reduce(Iterator.Element()) { >> var newResult = $0 >> newResult.insert($1) >> return newResult >> } >> } >> >> } >> >> extension Collection where Iterator.Element == OptionSet { >> >> func reduced<T: OptionSet>() -> T { >> return reduce(T()) { >> var newResult = $0 >> newResult.insert($1) >> return newResult >> } >> } >> >> } >> >> >> extension Collection where Iterator.Element == OptionSet { >> func reduced() -> Iterator.Element { >> return Iterator.Element(self) >> } >> } >> >> func reduced<T: OptionSet>(_ options: [T]) -> T { >> return options.reduce(T()) { >> var newResult = $0 >> newResult.insert($1) >> >> return newResult >> } >> } >> >> Jon Shier >> _______________________________________________ >> swift-users mailing list >> swift-users@swift.org <mailto:swift-users@swift.org> >> https://lists.swift.org/mailman/listinfo/swift-users >
_______________________________________________ swift-users mailing list swift-users@swift.org https://lists.swift.org/mailman/listinfo/swift-users