question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Access Enums in Swift more simple

See original GitHub issue

As I described here it is hard to access Java Enums from Swift. This can be done with:

var r:BISBTestTypeEnum = BISBTestTypeEnum.values().objectAtIndex(BISBTestType.Type1.rawValue) as! BISBTestTypeEnum

As you can see it is very long. It would be great if you could add one helper method to make such access more simple.

If you would add a method like this (Swift):

    class func withValue(value: BISBTestType) -> BISBTestTypeEnum {
        return BISBTestTypeEnum.values().objectAtIndex(value.rawValue) as! BISBTestTypeEnum
    }

in Objective-C I guess that is something like this:

+ (BISBTestTypeEnum *)valueOfWithNSString:(BISBTestType *)value {
  return [[BISBTestTypeEnum values] objectAtIndex:[value rawValue]];
}

you can access Enums as:

var r = BISBTestTypeEnum.withValue(BISBTestType.Type1)

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:15

github_iconTop GitHub Comments

1reaction
tomballcommented, May 18, 2015

Added --static-accessor-methods flag, which adds class methods to access static accessor functions and enum constants from Swift. This should now work:

var r:BISBTestTypeEnum = BISBTestTypeEnum.Type1()

0reactions
confilecommented, Jun 3, 2015

@kstanger This issue is solved with your last fix.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Enumerations — The Swift Programming Language (Swift 5.7)
An enumeration defines a common type for a group of related values and enables you to work with those values in a type-safe...
Read more >
Swift enums: An overview with examples - LogRocket Blog
Swift enums can be a powerful way to simplify your code. In this tutorial, we cover the basics of enums and how to...
Read more >
Enumerations - a free Hacking with Swift tutorial
Enumerations – usually just called “enum” and pronounced “ee-num” - are a way for you to define your own kind of value in...
Read more >
Five powerful, yet lesser-known ways to use Swift enums
A look at a few somewhat lesser-known ways in which enums can be used to solve various problems in Swift-based apps and libraries....
Read more >
Using Enums in Swift. How to handle the constants, raw…
In Swift, you can set data types and values ​​in Enums and define methods or properties on them. Also, using Codable, it is...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found