u16suzuの blog

日々学んだことのメモブログです。

UIWebView でリンクをクリックしたとき safari で開く

UIWebView でリンクをクリックしたとき safari で開く

func webView(webView: UIWebView,
  shouldStartLoadWithRequest request: NSURLRequest,
  navigationType: UIWebViewNavigationType) -> Bool {

    if(navigationType == UIWebViewNavigationType.LinkClicked){
        UIApplication.sharedApplication().openURL(request.URL!)
        return false
    }

    return true
}

swiftでKVO

  1. 監視対象のプロパティに dynamic 修飾子をつける
  2. Observer は NSObject を継承している必要がある
dynamic var fbNativeAd:FBNativeAd?        

addObserver(self, forKeyPath: "fbNativeAd", options: [.New, .Old], context: nil)
  
deinit {
     removeObserver(self, forKeyPath: "fbNativeAd")
 }
    
override internal func observeValueForKeyPath(
    keyPath: String?,
    ofObject object: AnyObject?,
    change: [String : AnyObject]?,
    context: UnsafeMutablePointer<Void>)
    {
        print("hoge-----")
        if( keyPath=="fbNativeAd"){
        }
}

swiftのディープコピー

swift はデフォルトで deep copyになる。 また、引数で受け取ったオブジェクトはletになってて直接加工できない。 そのため、ディープコピーを作ってそれを編集する必要がある。

// swift はデフォルトで deep copyになる
let ar = [1,2,3,4]

var ar2 = ar

ar2.insert(99, atIndex: 1)

print( ar, ar2)
// => [1, 2, 3, 4] [1, 99, 2, 3, 4]



// swiftでは引数で受け取ったオブジェクトを加工できない
func myInsert( ar:Array<Int> ) -> Array<Int>{
    var a = ar // いったんディープコピーする必要がある
    a.insert(55, atIndex: 1)
    return a
}


// let ar3 = [6,7,8,9]
var ar3 = [6,7,8,9]

print( myInsert(ar3) )
// => [6, 55, 7, 8, 9]
print(ar3)
// => [6, 7, 8, 9]

swiftのジェネリクスを使ってみる

    // ジェネリクス関数
    func swapAnyObject<T>(a:T, b:T){
        print( a.dynamicType, b.dynamicType)
    }

    // ジェネリクス関数その2
    // http://qiita.com/mini_house/items/73cefa9612d5c7680d7e より
    func myRepeat<ItemType>(item: ItemType, times: Int) -> [ItemType]{
        var result = [ItemType]()
        
        for _ in 0...times {
            result.append(item)
        }
        return result
    }
    
    
    // 型制約の例 Tを Equatable protocol を採用したクラスに制限している
    // http://qiita.com/mini_house/items/73cefa9612d5c7680d7e より
    func findIndex<T: Equatable>(array: [T], valueToFind: T) -> Int? {
        for (i,v) in array.enumerate() {
            if v == valueToFind {
                return i
            }
        }
        return nil
    }

Rubyで設定系DSLを書いてみた

こちらのスライドを参考にしてRuby で設定系のDSLを書いてみました。

sixeight.hatenablog.com

ところどころ、コメントを書いて理解しながら書いてみました。

# 設定系DSL

class Awesome
  class Config
    attr_accessor :hoge, :fuga # (a)
    attr_accessor :method
  end

  def self.configure(&block)
    block.call(self.config) # (2). @config オブジェクトを引数にして ブロックを実行
    # call を省略してもOK
    # block.(self.config)
  end

  # (1). Config class の @config オブジェクトを生成する
  def self.config
    @config ||= Config.new
  end
end

Awesome.configure do |c| # (3). このブロック引数に @config が渡されている
  c.hoge = 123 # (4). @config オブジェクトの (a). で定義されている hoge に代入される
  c.fuga = 456
  c.method = lambda{ p "hi" } # (5). メソッドも設定ファイルないで指定できる
end

p Awesome.config.hoge
p Awesome.config.fuga
p Awesome.config.method
Awesome.config.method.call() # (6). (5)でlambdaとして保存したメソッドを実行

Railsを使っているとよく出てくる設定ファイルがどのような仕組みで成り立っているのか分からなくて 気持ち悪かったのですが、 これでスッキリしました。