例外を投げるclosure
() throws ->(String) in
と書くと例外を投げるクロージャを定義できる
enum MyError : ErrorType { case Hoge } let c = {() throws ->(String) in throw MyError.Hoge } do { try c() } catch { print( "\(__FUNCTION__): L\(__LINE__)") }
Swiftでクラスからインスタンスを生成する
インスタンス化するクラスを定義
class Hoge { required init(){ } class func newInstance()->Hoge { return Hoge.init() } }
呼び出し
let hogeClass: Hoge.Type = Hoge.self let hogeObject = hogeClass.init()
Rubyで行列とベクトルの演算
固有ベクトルは任意の行列m を適用した際に,向きが変わらないベクトルのこと
require 'matrix' m = Matrix[ [2,1], [1,2] ] v1 = Vector[1, 1] v2 = Vector[1, -1] # v1は m の eigen vecotr p m * v1 # => Vector[3, 3] # v2 is eigen vector of matrix m too. # eigen value is 1 p m * v2 # => Vector[1, -1]
参考
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 }
UIView の bounds と frame プロパティについて
swiftでKVO
- 監視対象のプロパティに
dynamic
修飾子をつける - 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]