【Swift】iOS開發筆記(一)

 

前言ios

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

 

聲明
  歡迎轉載,但請保留文章原始出處:)
  博客園:http://www.cnblogs.com
  農民伯伯: http://over140.cnblogs.comui

 

一、隱藏/顯示密碼功能編碼

  光設置secureTextEntry還不行,你會發現UITextField在切換到顯示密碼時會多一個空字符,看着巨彆扭,須要在更改secureTextEntry後進行以下設置:spa

        let pwd = psdField.text
        self.psdField.text = pwd +  "   "
        self.psdField.text = pwd

 

二、獲取當前類的名稱code

String.fromCString(object_getClassName(self))

  注意:經過_stdlib_getDemangledTypeName也能取到,可是若是在父類裏面去就只能取到父類的名稱blog

 

三、 國際化開發

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

  凡是使用了NSLocalizedString的字符串都能被找到,支持子目錄查找,注意替換en.lprojget

 

四、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
        }

    }

 

五、 格式化數字輸出 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 ";
    }

 

六、 判斷屏幕是不是橫屏

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

 

七、 URL 編碼

text.stringByAddingPercentEncodingWithAllowedCharacters(.URLHostAllowedCharacterSet())

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

相關文章
相關標籤/搜索