【Swift】iOS開發小坑歷險記(一)

KristenShar 10年前發布 | 8K 次閱讀 Swift IOS Apple Swift開發

前言

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

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):

    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         }

    } </code></pre>

5、 格式化數字輸出 K/M

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";     }</code></pre>

6、 判斷屏幕是否是橫屏

  public static func isIsLandscape() -> Bool {
        return UIDeviceOrientationIsLandscape(UIDevice.currentDevice().orientation) || UIApplication.sharedApplication().statusBarOrientation == UIInterfaceOrientation.LandscapeLeft  || UIApplication.sharedApplication().statusBarOrientation == UIInterfaceOrientation.LandscapeRight
    }

7、 URL 編碼

text.stringByAddingPercentEncodingWithAllowedCharacters(.URLHostAllowedCharacterSet())

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

來源:農民伯伯

 本文由用戶 KristenShar 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!