Swift: Easy NS_OPTIONS creation
| Originator: | adrian.kashivskyy | ||
| Number: | rdar://17201969 | Date Originated: | 06-Jun-2014 07:58 PM |
| Status: | Dupe/15032042/Closed | Resolved: | Yes |
| Product: | Developer Tools | Product Version: | Xcode 6.0 Beta 5 (6A279r) |
| Classification: | Enhancement | Reproducible: | Not Applicable |
Summary:
It would be great if developers could easily create NS_OPTIONS equivalents in Swift. At the current moment, it is only opssible by creating a "struct : RawOptionSet" and the ading lots of boilerplate code, including overloaded bitwise operation functions and accessors.
Steps to Reproduce:
Expected Results:
Something relatively easy, line NS_OPTIONS in Objective-C.
Actual Results:
struct XYZOptions : RawOptionSet {
var value: UInt = 0
init(_ value: UInt) { self.value = value }
func toRaw() -> UInt { return self.value }
func getLogicValue() -> Bool { return self.value != 0 }
static func fromRaw(raw: UInt) -> XYZOptions? { return XYZOptions(raw) }
static func fromMask(raw: UInt) -> XYZOptions { return XYZOptions(raw) }
static var None: XYZOptions { return XYZOptions(0) }
static var FirstOption: XYZOptions { return XYZOptions(1 << 0) }
static var SecondOption: XYZOptions { return XYZOptions(1 << 1) }
static var ThirdOption: XYZOptions { return XYZOptions(1 << 2) }
}
func == (lhs: XYZOptions, rhs: XYZOptions) -> Bool { return lhs.value == rhs.value }
func | (lhs: XYZOptions, rhs: XYZOptions) -> XYZOptions { return XYZOptions(lhs.value | rhs.value) }
func & (lhs: XYZOptions, rhs: XYZOptions) -> XYZOptions { return XYZOptions(lhs.value & rhs.value) }
func ^ (lhs: XYZOptions, rhs: XYZOptions) -> XYZOptions { return XYZOptions(lhs.value ^ rhs.value) }
Version:
Feature is not yet implemented in iOS 8.0 SDK (12A4265u)
Notes:
My idea is to create a @bitmask attribute and somehow use it to denote a bitmask enum.
Idea #1:
@bitmask struct XYZOptions {
static var OptionOne { return XYZOptions(1 << 0) }
static var OptionTwo { return XYZOptions(1 << 1) }
}
Idea #2:
struct XYZOptions {
@bitmask(1 << 0) static var OptionOne
@bitmask(1 << 1) static var OptionTwo
}
Of course, these are only ideas. :)
Configuration:
Attachments:
--- 11-Jun-2014 01:25 AM
Engineering has determined that your bug report is a duplicate of another issue and will be closed. 15032042
Comments
Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!