Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
This repository was archived by the owner on Sep 6, 2019. It is now read-only.
/NataliePublic archive

Natalie - Storyboard Code Generator (for Swift)

License

NotificationsYou must be signed in to change notification settings

krzyzanowskim/Natalie

Repository files navigation

Natalie - Storyboard Code Generator (for Swift)

Swift

Current codebase is Swift 4 compatible.

Swift 3.x code may be found fromswift3 branch

Swift 2.x code may be found fromswift2 branch

Swift 1.x code may be found fromswift2 branch

Synopsis

Natalie generates Swift code based on storyboard files to make work with Storyboards and segues easier. Generated filereduce usage of Strings as identifiers for Segues or Storyboards.

Proof of concept implementation to address the String issue for strongly typed Swift language. Natalie is a Swift command-line application (written in Swift) that produces a single.swift file with a bunch of extensions to project classes along the generated Storyboard enum.

Natalie is written in Swift and requires Swift to run. The project usesSWXMLHash as a dependency to parse XML and due to framework limitations.

Enumerate Storyboards

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

structStoryboards{structMain{...}structSecond{...}...

Instantiate initial view controller for storyboard

letvc=Storyboards.Main.instantiateInitialViewController()

Instantiate ScreenTwoViewController in storyboard, using storyboard id

letvc=Storyboards.Main.instantiateScreenTwoViewController()

example usage for prepareForSegue()

overridefunc prepareForSegue(segue:UIStoryboardSegue, sender:AnyObject?){if segue==MainViewController.Segue.ScreenOneSegue{letviewController= segue.destinationViewControlleras?MyViewController    viewController?.view.backgroundColor=UIColor.yellowColor()}}

...it could beswitch { } statement, butit's broken.

Segues

Perform segue

self.perform(segue:MainViewController.Segue.ScreenOneSegue, sender:nil)

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

Segue enumeration contains list of available segues

kind property represent types Segue

destination property return type of destination view controller.

extensionMainViewController{enumSegue:String,Printable,SegueProtocol{case ScreenOneSegueButton="Screen One Segue Button"case ScreenOneSegue="ScreenOneSegue"varkind:SegueKind?{...}vardestination:UIViewController.Type?{switch(self){case ScreenOneSegueButton:returnScreenOneViewController.selfcase ScreenOneSegue:returnScreenOneViewController.selfdefault:assertionFailure("Unknown destination")returnnil}}varidentifier:String{returnself.description}vardescription:String{returnself.rawValue}}}

Reusable Views To Improve Performance

Collections and tables views usereuseidentifier on cell to recycle a view.

If you define it, their custom view controllers will be extended with aReusable enumeration, which contains list of available reusable identifiers

example to dequeue a view withReusable enumeration withUITableView:

func tableView(tableView:UITableView, cellForRowAtIndexPath indexPath:NSIndexPath)->UITableViewCell{letcell= tableView.dequeueReusableCell(ScreenTwoViewController.Reusable.MyCell, forIndexPath: indexPath)as!UITableViewCell    cell.textLabel?.text="\(indexPath.row)"return cell}

Before dequeuing your view, you must register a class or a xib for each identifier.If your cell view has custom class defined in storyboard, in your controller you can call directly

overridefunc viewDidLoad(){    tableView.registerReusableCell(MainViewController.Reusable.MyCell)}

You can pass the view instead - the view must define thereuseidentifier

    tableView.registerReusableCell(tableViewCell)

If your reusable custom view, you can also execute code according to reusable values

classMyCustomTableViewCell:UITableViewCell{overridefunc prepareForReuse(){ifself==MyCustomTableViewController.Reusable.MyCell{...}elseifself==MyCustomTableViewController.Reusable.mySecondCellId{...}}}

Colors (iOS 11, macOS 10.13)

Generate anUIColor (orNSColor) static property for each asset colors used in your storyboard.

Installation

Swift Package Manager

$ git clone https://github.com/krzyzanowskim/Natalie.git$ cd Natalie$ ./scripts/build.sh$ Binary at path ./Natalie/natalie

if you want easy Xcode integration you may want to install the binary to be easily accessible for any application from/usr/local/bin

$ cp natalie /usr/local/bin

Homebrew

$ brew install natalie

You can also putnatalie executable file at the root of your project folder and keep it under version control. This way everyone even your CI will be able to generate the files.

Xcode Integration

Natalie can be integrated with Xcode in such a way that theStoryboards.swift the 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 withNew Run Script Phase onBuild 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.

  • Select the project in the Project Navigator on the left of your Xcode window
  • Select your App Target in the list
  • Go in the "Build Phases" tab
  • Click on the "+" button on the upper left corner and choose "New Run Script Phase" and copy/paste script:
# Adjust path to "natalie" binary# NATALIE_PATH="$PROJECT_DIR/natalie"NATALIE_PATH="/usr/local/bin/natalie"if [-f$NATALIE_PATH ];thenecho"Natalie Generator: Determining if generated Swift file is up-to-date."        BASE_PATH="$PROJECT_DIR/$PROJECT_NAME"    OUTPUT_PATH="$BASE_PATH/Storyboards.swift"if [!-e"$OUTPUT_PATH" ]|| [-n"$(find"$BASE_PATH" -type f -name"*.storyboard" -newer"$OUTPUT_PATH" -print -quit)" ];thenecho"Natalie Generator: Generated Swift is out-of-date; re-generating..."        /usr/bin/chflags nouchg"$OUTPUT_PATH""$NATALIE_PATH""$BASE_PATH">"$OUTPUT_PATH"        /usr/bin/chflags uchg"$OUTPUT_PATH"echo"Natalie Generator: Done."elseecho"Natalie Generator: Generated Swift is up-to-date; skipping re-generation."fielseecho"error: Could not find Natalie Generator at$NATALIE_PATH; Please visit https://github.com/krzyzanowskim/Natalie for installation instructions."exit 1fi
  • addStoryboards.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 command 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 NatalieExample/NatalieExample/Base.lproj/Main.storyboard > NatalieExample/NatalieExample/Storyboards.swift

Contribution

Please submit Pull Request against current development branch.

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 copyof this software and associated documentation files (the "Software"), to dealin the Software without restriction, including without limitation the rightsto use, copy, modify, merge, publish, distribute, sublicense, and/or sellcopies of the Software, and to permit persons to whom the Software isfurnished to do so, subject to the following conditions:

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

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS ORIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THEAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHERLIABILITY, 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 THESOFTWARE.


[8]ページ先頭

©2009-2025 Movatter.jp