Passion make things more better

Ruby on Rails / React.js / Swift / AWS / Docker

SwiftでUIImagePickerControllerを使用したテンプレ

import UIKit

class UIPickerViewSampleViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        selectImage()
    }
    
    private func selectImage() {
        let alertController = UIAlertController(title: "画像を選択", message: nil, preferredStyle: .ActionSheet)
        let cameraAction = UIAlertAction(title: "カメラを起動", style: .Default) { (UIAlertAction) -> Void in
            self.selectFromCamera()
        }
        let libraryAction = UIAlertAction(title: "カメラロールから選択", style: .Default) { (UIAlertAction) -> Void in
            self.selectFromLibrary()
        }
        let cancelAction = UIAlertAction(title: "キャンセル", style: .Cancel) { (UIAlertAction) -> Void in
            self.dismissViewControllerAnimated(true, completion: nil)
        }
        alertController.addAction(cameraAction)
        alertController.addAction(libraryAction)
        alertController.addAction(cancelAction)
        presentViewController(alertController, animated: true, completion: nil
    }
    
    private func selectFromCamera() {
        if UIImagePickerController.isSourceTypeAvailable(.Camera) {
            let imagePickerController = UIImagePickerController()
            imagePickerController.delegate = self
            imagePickerController.sourceType = UIImagePickerControllerSourceType.Camera
            imagePickerController.allowsEditing = true
            self.presentViewController(imagePickerController, animated: true, completion: nil)
        } else {
            print("カメラ許可をしていない時の処理")
        }
    }
    
    private func selectFromLibrary() {
        if UIImagePickerController.isSourceTypeAvailable(.PhotoLibrary) {
            let imagePickerController = UIImagePickerController()
            imagePickerController.delegate = self
            imagePickerController.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
            imagePickerController.allowsEditing = true
            self.presentViewController(imagePickerController, animated: true, completion: nil)
        } else {
            print("カメラロール許可をしていない時の処理")
        }
    }
    
    func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
        if let image = info[UIImagePickerControllerEditedImage] {
            print(image)
        }
        picker.dismissViewControllerAnimated(true, completion: nil)
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

CocoaPodsでpod installしたらundefined method 'to_ary'に出くわした

アプリを作っていて、新しいライブラリを導入するためにいつものようにPodfileに書いて、pod installしたら掲題のエラーが起きました。 他のライブラリでは問題なくできていたのに急に!?という思いに駆られ、調べていたところ、以下のissueが見つかりました。

undefined method `to_ary' #4891

読んでみるとどうやらrubyのバージョンが2.3以上の場合、起きることがあるようだ、ということがわかり、rubyのバージョンを調べてみると案の定。

╰─$ ruby -v
ruby 2.3.0p0 (2015-12-25 revision 53290) [x86_64-darwin14]

対応策としてはCocoaPods 1.0.0をインストールすることらしい。まだβ版(2016/4/2時点)なのでうーん、と思いながらアップデート作業。

// なんかあると怖そうだったので一旦消す
~ gem uninstall cocoapods
~ gem install cocoapods --pre
~ pod setup

そして再度pod installを実行すると見事成功。 ちょっとハマったので他の人の役に立てればと。

Rails runnerを使ってスクリプトを実行する

Railsを使っている時に、普通の処理とは別だけどもRailsの機能も使いつつ単独のスクリプト実行したい(バッチ処理とか、アプデに伴う諸々のデータの更新など)、と思ったことがある人は多いのではないでしょうか?そんな時に役立つのがRails runnerです。

はじめに

railsのルートディレクトリ直下にscriptsというフォルダを用意し、その中に実行したいファイルを作成します。 今回は20160322_passion.rbというファイルを作成します。内容は以下です。この中のスクリプトRailsの機能も使用可能です。

class Passion
  def self.execute
    put 'Do you have passion?'
  end
end

Passion.execute

スクリプトを実行する

作成したスクリプトは、以下のコマンドで実行します。

~ bundle exec rails runner scripts/20160322_passion.rb

環境毎に実行したい時

stagingやproduction環境下でも実行したい場合があるでしょう。その場合は以下のように-eオプションをつけて実行します。

bundle exec rails runner -e environment_here scripts/20160322_passion.rb

参考

gitでmerge済みのlocal branchを一括で削除する

よくローカルに溜め込みすぎて、毎度調べているので、自分用のメモとして。

git branch --merged | grep -v '^*' | xargs git branch -d

参考

SwiftでxibをカスタムViewとして使用する時の諸々

SwiftでStoryboard等を使っている場合、すべてのViewに関する処理を、同一のViewControllerに書くと、とても長くなってしまいます。そのため、Viewの切り出しを行う方が多いかと思います。以下でその分割方法について書いていきます。

xibをカスタムViewとして使用する方法

  1. UIView(以下CustomView.swiftと呼ぶ)とxib(以下CustomView.xibと呼ぶ)のファイルを作成する
  2. CustomView.xibで配置したViewのFile's OwnerにCustomViewを指定する
  3. CustomView.xibで配置したViewをCustomView.swiftcontentViewという名前で紐付ける
  4. CustomView.swiftにxibを読み込むための処理を記述する(以下参照)
import UIKit

class CustomView: UIView {

    @IBOutlet var contentView: UIView!
        
    required init(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)!
        loadXib()
    }
    
    private func loadXib() {
        NSBundle.mainBundle().loadNibNamed("CustomView", owner: self, options: nil)
        contentView.frame = CGRectMake(0, 0, frame.size.width, frame.size.height)
        addSubview(contentView)
    }
}

あとは実際に使うところでUIViewのClassをCustomViewにすれば動くかと思います。

参考