[Swift]クロージャ

クロージャ objective-cにおけるブロック

宣言

typealias ChangeMonthClosureType = (year:Int,month:Int) -> () //型に別名を付ける
var closureOnChangeMonth:ChangeMonthClosureType?

設定

self.msvc.closureOnChangeMonth = {
    (year:Int,month:Int) -> () in
    println("closureOnChangeMonth year:\(year) month:\(month)")
}

実行

self.closureOnChangeMonth?(year: self._year, month: self._month)

[Swift]参照渡し

参照渡し

class func addMonthWithYearMonth(inout ym:(year:Int,month:Int),addMonth:Int){
    var y:Int = ym.year
    var m:Int = ym.month + addMonth
    while 12 < m {
        m -= 12
        y++
    }
    while m < 1 {
        m += 12
        y--
    }
    ym.year=y
    ym.month=m
}

[Swift]sort

sort

var w:[AnyObject] = self.subviews
w.sort { (v1:AnyObject, v2:AnyObject) -> Bool in
    return v1.tag < v2.tag
}

[Swift]クラス拡張

クラス拡張 objective-cにおけるカテゴリ

extension UIView {
    var left:CGFloat{
        get{
            return self.frame.origin.x
        }
        set{
            self.frame = CGRectMake(newValue, self.frame.origin.y, self.frame.size.width, self.frame.size.height)
        }
    }
    func toUIImage() -> UIImage{
        UIGraphicsBeginImageContextWithOptions(self.bounds.size, false, 0.0)
        self.layer.renderInContext(UIGraphicsGetCurrentContext())
        let image:UIImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return image
    }
}