Help us understand the problem. What is going on with this article?

StoryBoardのIdentifier地獄を解決するSBConstants

More than 5 years have passed since last update.

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

susieyy
フリーランス - スタートアップエンジニアリングアドバイザー - iOS技術顧問 - プロトタイプ開発
https://susieyy.com
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした