Swift 常用技術總結

特定導航欄隱藏

override func viewDidLoad() {
     super.viewDidLoad()
     self.navigationController?.delegate = self
 }
extension MLMineTableViewController:UINavigationControllerDelegate {
    func navigationController(_ navigationController: UINavigationController, willShow viewController: UIViewController, animated: Bool){
     let vcClass =  viewController.isKind(of:       
     MLMineTableViewController.superclass()!)
     self.navigationController?.navigationBar.isHidden = vcClass
     navigationController.setNavigationBarHidden(vcClass, animated: animated)
    }
}

ScrollView,TableView滑動鍵盤隱藏

scrollView.keyboardDismissMode = .OnDrag
or
scrollView.keyboardDismissMode = .Interactive

浮點數取整

public func ceil(_: Double) -> Double  //取上整
public func floor(_: Double) -> Double //取下整

TableView的cell自適應高度,不用單獨設置每個cell的高度

 self.tableView.rowHeight = UITableViewAutomaticDimension
 self.tableView.estimatedRowHeight = 200; //預計高度

枚舉類型

@objc enum DrugStoreType: Int {
    case undefined = 0
    case online = 1
    case offlinee = 2
    var text: String {
        switch self {
        case .online:
            return "線上藥店"
        case .offlinee:
            return "線下藥店"
        default:
            return ""
        }
    }
}
let content = DrugStoreType.undefined.text  // 直接顯示枚舉對應的文本

泛型

任務:打印輸出數組內所有的元素。

var stringArray = ["蒼老師", "范老師", "優衣庫"]
var intArray = [1, 3, 4, 5, 6]
var doubleArray = [1.0, 2.0, 3.0]
func printStringArray(a: [String]) {         
  for s in a {              
   print(s)        
   } }
func printIntArray(a: [Int]) { 
  for i in a { 
    print(i) 
   } }
func printDoubleArray(a: [Double]) {for d in a { print(d) } }

簡介版

func printElementFromArray(a: [T]) {        
    for element in a {            
      print(element)        
     } }
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容