krzyzanowskim / Natalie

Natalie - Storyboard Code Generator (for Swift)
http://blog.krzyzanowskim.com/2015/04/15/natalie-storyboard-code-generator/
MIT License
1.17k stars 74 forks source link

Conflict when Storyboard filename matches UIViewController subclass name #129

Open yoiang opened 6 years ago

yoiang commented 6 years ago

A project I am working on correlates our Storyboard file's names with the UIViewController subclass that is it's initial view controller. The result is a Storyboards.* struct that conflicts in naming with the view controller type itself:

...
    struct ExampleViewController: Storyboard {

        static let identifier = "ExampleViewController"

        static var storyboard: UIStoryboard {
            return UIStoryboard(name: self.identifier, bundle: nil)
        }

        static func instantiateExampleViewController() -> ExampleViewController {
            return self.storyboard.instantiateExampleViewController() as! ExampleViewController
        }

        static func instantiateViewController(withIdentifier identifier: String) -> UIViewController {
            return self.storyboard.instantiateViewController(withIdentifier: identifier)
        }

        static func instantiateViewController<T: UIViewController>(ofType type: T.Type) -> T? where T: IdentifiableProtocol {
            return self.storyboard.instantiateViewController(ofType: type)
        }
    }
...

I appreciate the brevity of the struct's name and appending something like "File" clutters that. On the other hand it is more explicit: these are mappings to files as much as we'd love to abstract them away.

phimage commented 6 years ago

There is no compile issue? because there is Storyboards.ExampleViewController vs ExampleViewController

The only things we can do is apply naming rules according to some options, like adding a postfix (like Storyboard) or remove some part like ViewController

What are you expected?

yoiang commented 6 years ago

There is a compilation issue because of that conflict.

I would expect the resulting code to be an expected, consistent output, ie: not to do a different naming convention for some VCs because they conflict but not for others because they don't. Applying a naming rule or removing parts may decrease the likelihood of a conflict but it will still be a possibility.

Other possibility, with Swift we can specify the specific ExampleViewcontroller we are referencing if we know the project's module name:

...
    struct ExampleViewController: Storyboard {

...
        static func instantiateExampleViewController() -> ExampleModule.ExampleViewController {
            return self.storyboard.instantiateExampleViewController() as! ExampleModule.ExampleViewController
        }
...
    }
...
phimage commented 6 years ago

so if there is a compile issue, please provide the error text or a test project it will be easier to understand

ps: we have already discuss in an other issue about id naming. (no time to search now)