天天看點

【Swift】iOS開發曆險記(一)

前言

  邊開發邊學習,邊攢經驗,彙總一下記錄到這裡

聲明 

歡迎轉載,但請保留文章原始出處:) 

部落格園:http://www.cnblogs.com

農民伯伯: http://over140.cnblogs.com

1、隐藏/顯示密碼功能

  光設定securetextentry還不行,你會發現uitextfield在切換到顯示密碼時會多一個空字元,看着巨别扭,需要在更改securetextentry後進行如下設定:

        let pwd = psdfield.text

        self.psdfield.text = pwd + " "

        self.psdfield.text = pwd

2、擷取目前類的名稱

string.fromcstring(object_getclassname(self))

  注意:通過_stdlib_getdemangledtypename也能取到,但是如果在父類裡面去就隻能取到父類的名稱

3、 國際化

find . \( -name '*.m' -o -name '*.h' \) -print0 | xargs -0 genstrings -o en.lproj 

  凡是使用了nslocalizedstring的字元串都能被找到,支援子目錄查找,注意替換en.lproj

4、uitableview分割線的顯示問題

  去掉分割線:設定uitableview的separatorstyle = uitableviewcellseparatorstyle.none

  去掉多餘的分割線:設定uitableview的tablefooterview = uiview()  (要是不設定會很醜,不管有沒有資料都會顯示分割線)

  處理 ios8 分割線左邊距設定為0失效的問題,參考這裡(http://stackoverflow.com/questions/25770119/ios-8-uitableview-separator-inset-0-not-working):

【Swift】iOS開發曆險記(一)

    func tableview(tableview: uitableview, willdisplaycell cell: uitableviewcell, forrowatindexpath indexpath: nsindexpath) {

        // remove seperator inset

        if cell.respondstoselector("setseparatorinset:") {

            cell.separatorinset = uiedgeinsetszero

        }

        // prevent the cell from inheriting the table view's margin settings

        if cell.respondstoselector("setpreservessuperviewlayoutmargins:") {

            cell.preservessuperviewlayoutmargins = false

        // explictly set your cell's layout margins

        if cell.respondstoselector("setlayoutmargins:") {

            cell.layoutmargins = uiedgeinsetszero

    }

【Swift】iOS開發曆險記(一)

5、 格式化數字輸出 k/m

【Swift】iOS開發曆險記(一)

extension string {

    public func substring(startindex: int, endindex: int) -> string{

        return (self as nsstring).substringwithrange(nsrange(location: startindex, length: endindex - startindex))

}

    public static func prettynumber(num: double) -> string{

        if (num < 10000) {

            return "\(int(num))";

        } else if (num < 100000) {

            return "\(num / 1000.0)".substring(0, endindex: 4) + "k"

        } else if (num < 1000000) {

            return "\(num / 1000.0)".substring(0, endindex: 3) + "k"

        } else if (num < 100000000) {

            return "\(num / 1000000.0)".substring(0, endindex: 4) + "m"

        } else if (num < 1000000000) {

            return "\(num / 1000000.0)".substring(0, endindex: 3) + "m"

        } else if (num < 100000000000) {

            return "\(num / 1000000000.0)".substring(0, endindex: 4) + "m"

        } else if (num < 1000000000000) {

            return "\(num / 1000000000.0)".substring(0, endindex: 3) + "m"

        return "inf";

【Swift】iOS開發曆險記(一)

6、 判斷螢幕是否是橫屏

7、 url 編碼

   這個 text 的類型是 string ,常用于搜尋功能,在  url 中包含被搜的關鍵字,如果不處理搜中文或者帶空格的英文會直接崩潰

轉載:http://www.cnblogs.com/over140/p/4571398.html