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

Extend enums to have count #1

Closed
3lvis opened this issue Aug 28, 2016 · 3 comments
Closed

Extend enums to have count #1

3lvis opened this issue Aug 28, 2016 · 3 comments

Comments

@3lvis
Copy link
Collaborator

3lvis commented Aug 28, 2016

extension RawRepresentable where RawValue: IntegerType {
    static var count: Int {
        var i: RawValue = 0
        while let _ = Self(rawValue: i) {
            i = i.successor()
        }
        return Int(i.toIntMax())
    }
}
@3lvis
Copy link
Collaborator Author

3lvis commented Aug 28, 2016

"Hey, but this doesn't work with String types!"

Doesn't matter, you shouldn't be using that anyway since it doesn't provide you a way to return localized variations. So maybe this should be better.

enum Section: Int {
    case title, price, others, delete

    func toString() -> String? {
        switch self {
        case .title:
            return NSLocalizedString("Title", comment: "")
        case .price:
            return NSLocalizedString("Price", comment: "")
        case .others:
            return NSLocalizedString("Others", comment: "")
        case .delete:
            return nil
        }
    }
}

@elland
Copy link
Contributor

elland commented Aug 30, 2016

💃

@3lvis
Copy link
Collaborator Author

3lvis commented Aug 31, 2016

Fixed on #2

@3lvis 3lvis closed this as completed Aug 31, 2016
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