StoryBoardのIdentifier地獄を解決するSBConstants



![2015_01_12_2_23.jpg](https://qiita-image-store.s3.amazonaws.com/0/4943/b079f460-0146-6b38-38ae-cbb0eef95c24.jpeg "2015_01_12_2_23.jpg")


## はじめに

StoryBoardによりViewを作りこんでいく作業は大変便利になりました。
PropertyやActionをコードへマッピングするのもドラッグ＆ドロップで簡単に行えます。
ですが、IdentifierはStoryBoardで設定した文字列を、プラグラマーが誤字なくコード上で記述しなくてはなりません。

そこで[SBConstants](https://github.com/paulsamuels/SBConstants)を利用すると、設定したIdentifierから自動的に定数コードを作成し、誤字や定数化の作業（地獄）から開放されます。

## 実行環境

+ Xcode 6.1.1 (6A2008a)
+ cocoapods 0.35.0
+ sbconstants 1.0.0

## サイト

+ Github https://github.com/paulsamuels/SBConstants
+ Blog http://paul-samuels.com/blog/2013/01/31/storyboard-constants/

## インストール

rubyのgemでインストールします。

```Shell
$ gem install sbconstants
```

## 使い方

以下ヘルプです。

```Shell
$ sbconstants -h
Usage: DESTINATION_FILE [options]
    -p, --prefix=<prefix>            Only match identifiers with <prefix>
    -s, --source-dir=<source>        Directory containing storyboards
    -q, --queries=<queries>          YAML file containing queries
    -d, --dry-run                    Output to STDOUT
    -v, --verbose                    Verbose output
```


    
## 使用例 (ObjC)

http://paul-samuels.com/blog/2013/01/31/storyboard-constants/ より

予め、PASStoryboardConstants.(h|m)ファイルをXcode上に手動で追加しておきます。

```Shell
$ sbconstants MyApp/Constants/PASStoryboardConstants.h --source-dir MyApp/Storyboards
```
（ 上記コマンドを build phases に設定するとビルド毎に更新できます ）


PASStoryboardConstants.h

```ObjC
// Auto generated file - any changes will be lost

#pragma mark - tableViewCell.reuseIdentifier
extern NSString * const PSBAwesomeCell;  

#pragma mark - segue.identifier
extern NSString * const PSBMasterToDetail;  
extern NSString * const PSBMasterToSettings;
```

PASStoryboardConstants.m

```ObjC
// Auto generated file - any changes will be lost

#import "PASStoryboardConstants.h"

#pragma mark - tableViewCell.reuseIdentifier
NSString * const PSBAwesomeCell = @"PSBAwesomeCell";

#pragma mark - segue.identifier
NSString * const PSBMasterToDetail = @"PSBMasterToDetail";
NSString * const PSBMasterToSettings = @"PSBMasterToSettings";
```


## 使用例 (Swift)

https://github.com/artsy/eidolon より

ObjCに使用例に加えて、`--swift`オプションを追記します。

```Shell
$ sbconstants Kiosk/Storyboards/StoryboardIdentifiers.swift --source-dir Kiosk/Storyboards --swift
```

StoryboardIdentifiers.swift

```Swift
// Auto generated file from SBConstants - any changes may be lost

public enum SegueIdentifier : String {
    case ArtsyUserHasNotRegisteredCard = "Artsy User Has Not Registered Card"
    case ArtsyUserviaPINHasNotRegisteredCard = "Artsy User via PIN Has Not Registered Card"
    case ConfirmBid = "Confirm Bid"
    case ConfirmyourBidArtsyLogin = "Confirm your Bid - Artsy Login"
    case ConfirmyourBidBidderFound = "Confirm your Bid - Bidder Found"
    case ConfirmyourBidBidderNotFound = "Confirm your Bid - Bidder Not Found"
    case EmailLoginConfirmedHighestBidder = "Email Login Confirmed Highest Bidder"
    case EmailNotFoundonArtsy = "Email Not Found on Artsy"
    case ExistingArtsyUserFound = "Existing Artsy User Found"
    case LoadAdminWebViewController = "Load Admin Web View Controller"
    case PINConfirmedhasCard = "PIN Confirmed has Card"
    case PlaceAnotherBid = "Place Another Bid"
    case PlaceaHigherBidAfterNotBeingHighestBidder = "Place a Higher Bid After Not Being Highest Bidder"
    case PushtoBidConfirmed = "Push to Bid Confirmed"
    case RegisterCreditCardToPostal = "RegisterCreditCardToPostal"
    case RegisterPostalToConfirm = "RegisterPostalToConfirm"
    case ShowAdminOptions = "Show Admin Options"
    case ShowLoadingView = "Show Loading View"
    case ShowSaleArtworkDetails = "Show Sale Artwork Details"
    case ZoomIntoArtwork = "Zoom Into Artwork"
}

public enum ViewControllerStoryboardIdentifier : String {
    case AuctionListings = "AuctionListings"
    case CardTester = "Card Tester"
    case ConfirmYourBid = "Confirm Your Bid"
    case ConfirmYourBidArtsyLogin = "Confirm Your Bid Artsy Login"
    case ConfirmYourBidEnterEmail = "Confirm Your Bid Enter Email"
    case ConfirmYourBidEnterPassword = "Confirm Your Bid Enter Password"
    case ConfirmYourBidPIN = "Confirm Your Bid PIN"
    case FulfillmentContainer = "Fulfillment Container"
    case LoadingBidsorRegistering = "Loading Bids or Registering"
    case ManualCardDetailsInput = "Manual Card Details Input"
    case NoInternetConnection = "No Internet Connection"
    case PlaceYourBid = "Place Your Bid"
    case RegisterAnAccount = "Register An Account"
    case RegisterConfirm = "Register Confirm "
    case RegisterCreditCard = "Register Credit Card"
    case RegisterEmail = "Register Email"
    case RegisterMobile = "Register Mobile"
    case RegisterPassword = "Register Password"
    case RegisterPostalorZip = "Register Postal or Zip"
    case SaleArtworkDetail = "SaleArtworkDetail"
}
```

## REF

実際に使用されているOSSのアプリがあります。

+ https://github.com/artsy/eidolon