[Swift]クラス名取得

クラス名取得

設定

extension UIViewController {
    var className:String{
        get{
            return NSStringFromClass(self.dynamicType).componentsSeparatedByString(".").last!
        }
    }
}

実行

println("\(self.className) - \(__FUNCTION__) \(__LINE__) @ \(__FILE__)")

[Swift]IBOutlet,IBAction

IBOutlet,IBAction

@IBOutlet var button:UIButton?
@IBAction func onTapButton(sender:AnyObject){
    println("onTapButton")
    var button:UIButton = sender as UIButton
}

[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
}