You can currently access swift enums from objective c provided that it is marked @objc and inherits from Int.<br><br>Objectivec can not however access a swift enum that inherits from a string. I've asked the community about how they feel about enabling this but there was little to no response. <br><div class="gmail_quote"><div dir="ltr">On Thu, Dec 15, 2016 at 1:06 AM Charles Srstka via swift-evolution <<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">MOTIVATION:<br class="gmail_msg">
<br class="gmail_msg">
Many Cocoa classes which allow the user to choose from a list of items, such as NSPopUpButton, NSSegmentedControl, and NSTabView, offer the ability to bind the view to an integer or string in the model via KVO, through bindings such as “Selected Tag”, “Selected Index”, “Selected Identifier”, and the like. Since it can be tough to remember all the tags and whatnot that are associated with each view, it’s usually been helpful to create an enum to keep track of them… until now, since Objective-C cannot see Swift enums, and therefore they cannot be marked ‘dynamic’ for KVO.<br class="gmail_msg">
<br class="gmail_msg">
One can work around that by declaring two properties, one of which wraps the other, like this:<br class="gmail_msg">
<br class="gmail_msg">
enum SortMethod: Int {<br class="gmail_msg">
case byName = 0<br class="gmail_msg">
case bySize = 1<br class="gmail_msg">
case byModificationDate = 2<br class="gmail_msg">
}<br class="gmail_msg">
<br class="gmail_msg">
var sortMethod: SortMethod {<br class="gmail_msg">
willSet { self.willChangeValue(forKey: “sortMethod”) }<br class="gmail_msg">
didSet { self.didChangeValue(forKey: “sortMethod”) }<br class="gmail_msg">
}<br class="gmail_msg">
<br class="gmail_msg">
@objc(sortMethod) private dynamic var objcSortMethod: Int {<br class="gmail_msg">
get { return self.sortMethod.rawValue }<br class="gmail_msg">
set(newValue) { self.sortMethod = SortMethod(rawValue: newValue)! }<br class="gmail_msg">
}<br class="gmail_msg">
<br class="gmail_msg">
However, this is cumbersome.<br class="gmail_msg">
<br class="gmail_msg">
PROPOSED SOLUTION:<br class="gmail_msg">
<br class="gmail_msg">
I propose that if an property is typed to an enum, and that enum is backed by an Objective-C-bridgeable type, the property should be visible to Objective-C as its underlying type. So, for example, if you simply declared “var sortMethod: SortMethod” with the @objc attribute or the ‘dynamic’ keyword, it would generate the longer code shown above. This would allow easy binding of UI elements in .xib files to enums in your model.<br class="gmail_msg">
<br class="gmail_msg">
ALTERNATIVES CONSIDERED:<br class="gmail_msg">
<br class="gmail_msg">
We could introduce some sort of annotation allowing the user to specify a default case in the event that Objective-C tried to set the property to a value not covered in the enum, instead of just crashing. However, doing such a thing is almost always the result of a programmer error, so I do not consider this of high importance.<br class="gmail_msg">
<br class="gmail_msg">
Charles<br class="gmail_msg">
<br class="gmail_msg">
_______________________________________________<br class="gmail_msg">
swift-evolution mailing list<br class="gmail_msg">
<a href="mailto:swift-evolution@swift.org" class="gmail_msg" target="_blank">swift-evolution@swift.org</a><br class="gmail_msg">
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" class="gmail_msg" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br class="gmail_msg">
</blockquote></div>