Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Toolkit enhancement proposal: Swift CustomStringConvertible and CustomDebugStringConvertible for AGS Objective-C Enums #34

Open
esreli opened this issue May 10, 2018 · 2 comments

Comments

@esreli
Copy link

esreli commented May 10, 2018

I would like to introduce a swift toolkit enhancement that would adhere AGS enums to the swift CustomStringConvertible and CustomDebugStringConvertible protocols.

This enhancement will allow someone to either:

  1. Help display to a user a stringified status
  2. Display to a developer a stringified debug status in the debug console

This will allow a developer to more easily convey information contained in an Objective-C enum (statuses, etc) to themselves or to the user.

For example:

extension AGSDrawStatus: CustomStringConvertible {
    
    public var description: String {
        switch self {
        case AGSDrawStatusInProgress:
            return "In Progress"
        case AGSDrawStatusCompleted:
            return "Completed"
        default:
            return ""
        }
    }
}

extension AGSDrawStatus: CustomDebugStringConvertible {
    
    public var debugDescription: String {
        return "[Draw Status] \(self)"
    }
}

let drawStatus:AGSDrawStatus = AGSDrawStatusInProgress

print(drawStatus)
// In Progress

print(drawStatus.description)
// In Progress

print(drawStatus.debugDescription)
// [Draw Status] In Progress

and

(lldb) po drawStatus
▿ [Draw Status] In Progress
  - rawValue : 0
@philium
Copy link
Contributor

philium commented May 11, 2018

Remember that Swift imports Objective-C enum constants as members, so it would be case .inProgress and case .completed.

The default description for cases of a Swift enum is simply the case name:

enum DrawStatus {
    case inProgress
    case completed
}

print(DrawStatus.inProgress) // inProgress
print(DrawStatus.completed) // completed

What would you think of using the case name for the descriptions?

@esreli
Copy link
Author

esreli commented May 11, 2018

A heads up @philium, the above construct was based on a simplified proof of concept I had built, they aren't reflective of the actual SDK. I built this proof of concept because I won't be posting proprietary code in a public channel and thus the construct above does not reflect the actual codebase but instead a demo.

As for the stringified enum case, I would personally advocate for a proper description as opposed to printing the case name, it's something that can both be used in debugging as well as something that can be used in some sort of UI element.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants