Giter Site home page Giter Site logo

jai / natalie Goto Github PK

View Code? Open in Web Editor NEW

This project forked from krzyzanowskim/natalie

0.0 2.0 0.0 274 KB

Natalie - Storyboard Code Generator (for Swift)

Home Page: http://blog.krzyzanowskim.com/2015/04/15/natalie-storyboard-code-generator/

License: MIT License

Swift 100.00%

natalie's Introduction

Natalie

Natalie - Storyboard Code Generator (for Swift)

Synopsis

Script generate Swift code based on storyboard files to make work with Storyboards and segues easier. Generated file reduce usage of Strings as identifiers for Segues or Storyboards.

This is a proof of concept to address the String issue for strongly typed Swift language. Natalie is a Swift script (written in Swift) that produces a single .swift file with a bunch of extensions to project classes along the generated Storyboard enum.

Since Natalie is a Swift script, that means it is written in Swift and requires Swift to run. The project uses SWXMLHash as a dependency to parse XML and due to framework limitations all code is in a single file.

###Enumerate Storyboards Generated enum Storyboards with convenient interface (drop-in replacement for UIStoryboard).

enum Storyboards: String {
    case Main = "Main"
    case Second = "Second"
    ...

Instantiate ViewController for storyboard

let vc:MyViewController = Storyboards.Main.instantiateInitialViewController()

example usage for prepareForSegue()

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if let selection = segue.selection() {
        switch (selection, segue.destinationViewController) {
        case (MainViewController.Segue.ScreenOneSegue, let oneViewController as ScreenOneViewController):
            oneViewController.view.backgroundColor = UIColor.yellowColor()
        default: break
        }
    }
}

###Segues

Perform segue

self.performSegue(MyCustomViewController.Segue.goToDetails, sender:nil)

Each custom view controller is extended with this code and provide list of available segues and additional informations from Storyboard.

storyboardIdentifier is unique identifier of view controller in Storyboard

Segue enumeration contains list of available segues

kind property represent types Segue

destination property return type of destinated view controller.

extension MyCustomViewController { 
    var storyboardIdentifier:String { return "oEB-rK-hJd" }

    enum Segue: String, Printable {
        case goToDetails = "goToDetails"
        case expandGroup = "composeMessage"

        var kind: SegueKind? {
            ...
        }

        var destination: UIViewController.Type? {
            ...
        }

        var identifier: String { return self.description } 
        var description: String { return self.rawValue }
    }
}

##Installation

There is no need to do any installation, however if you want easy Xcode integration you may want to install the script to be easily accessible for any application from /usr/local/bin

$ git clone https://github.com/krzyzanowskim/Natalie.git
$ sudo cp natalie.swift /usr/local/bin/natalie.swift

###Xcode Integration

Natalie can be integrated with Xcode in such a way that the Storyboards.swift file will be updated with every build of the project, so you don't have to do it manually every time.

This is my setup created with New Run Script Phase on Build Phase Xcode target setting. It is important to move this phase above Compilation phase because this file is expected to be up to date for the rest of the application.

echo "Natalie generator"
/usr/local/bin/natalie.swift "$PROJECT_DIR/$PROJECT_NAME" > "$PROJECT_DIR/$PROJECT_NAME/Storyboards.swift"

Don't forget to add Storyboards.swift to the project.

##Usage:

Download Natalie from Github: https://github.com/krzyzanowskim/Natalie and use it in the console, for example like this:

$ git clone https://github.com/krzyzanowskim/Natalie.git
$ cd Natalie

The script expects one of two types of parameters:

  • path to a single .storyboard file
  • path to a folder

If the parameter is a Storyboard file, then this file will be used. If a path to a folder is provided Natalie will generate code for every storyboard found inside.

$ natalie.swift NatalieExample/NatalieExample/Base.lproj/Main.storyboard > NatalieExample/NatalieExample/Storyboards.swift

Author and contact

Marcin Krzyżanowski

Licence

The MIT License (MIT)

Copyright (c) 2015 Marcin Krzyzanowski

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

natalie's People

Contributors

krzyzanowskim avatar jai avatar phimage avatar chrisfsampaio avatar defrenz avatar shulepov avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.