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

[Tip] Note to self, don't do outer scope enum/struct declaration, try nested instead. #50

Open
vinhnx opened this issue Sep 18, 2017 · 0 comments

Comments

@vinhnx
Copy link
Owner

vinhnx commented Sep 18, 2017

Note to self, don't do outer scope enum/struct declaration, try nested instead.

Be friend with Swift's name-spacing (nested) feature to better code management.

// Preferred
class SomeClass {

  // MARK: ENUM

  enum SomeType {
    case foo, bar, baz, unknown
  }

  // MARK: PROPERTIES

  private var type = SomeType.unknown
}

Sometimes SomeType is supposed to be private. But if we declare outside of class/struct declaration, it is default to public scope.

To mark it private, we can use private (for inside this class or it's extension in Swift 4) or fileprivate (inside this class only); but that is not the point here.

So if we declare as nested. It means SomeClass owns SomeType enum, so callers need explicit call SomeClass.SomeType.

// Not Preferred

enum SomeType {  // exposed
 case foo, bar, baz, unknown
}

class SomeClass {

  // MARK: PROPERTIES

  private var type = SomeType.unknown
}

Reference: #46

🚀 👍

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

No branches or pull requests

1 participant