50
51

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

StoryBoardのIdentifier地獄を解決するSBConstants

Posted at

2015_01_12_2_23.jpg

はじめに

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

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

実行環境

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

サイト

インストール

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

$ gem install sbconstants

使い方

以下ヘルプです。

$ 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上に手動で追加しておきます。

$ sbconstants MyApp/Constants/PASStoryboardConstants.h --source-dir MyApp/Storyboards

( 上記コマンドを build phases に設定するとビルド毎に更新できます )

PASStoryboardConstants.h

// 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

// 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オプションを追記します。

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

StoryboardIdentifiers.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のアプリがあります。

50
51
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
50
51

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?