天天看點

【Swift】iOS開發筆記(一)

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

    }

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";

6、 判斷螢幕是否是橫屏

7、 URL 編碼

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