您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 程式開發 >> iOS 開發綜合

iOS 開發:Swift 常用全域函式與 extension 整理(持續更新)

編輯:iOS 開發綜合

以下整理了 iOS 開發中常用的 Swift 全域函式與 extension(因為 Swift 中無法使用巨集定義)。而在 Objective-C 中,其巨集定義的整理請參考部落格《iOS — 總結 Objective-C 中常用的巨集定義(持續更新中)》。

 // // Global.swift // // useful global methods and extension for Chris Hu // import Foundation func isIPhone4() -> Bool { return UIScreen.mainScreen().currentMode?.size == CGSizeMake(640, 960) } func isIPhone5() -> Bool { return UIScreen.mainScreen().currentMode?.size == CGSizeMake(640, 1136) } func isIPhone6() -> Bool { return UIScreen.mainScreen().currentMode?.size == CGSizeMake(750, 1334) } func isIPhone6Plus() -> Bool { return UIScreen.mainScreen().currentMode?.size == CGSizeMake(1242, 2208) } func isIPhone6PlusBigMode() -> Bool { return UIScreen.mainScreen().currentMode?.size == CGSizeMake(1125, 2001) } func currentLanguage() -> String { return NSLocale.preferredLanguages().first! } func systemVersion() -> String { return UIDevice.currentDevice().systemVersion } func appVersion() -> String { return String(NSBundle.mainBundle().infoDictionary!["CFBundleShortVersionString"]!) } extension UIColor { public convenience init(hexString: UInt32, alpha: CGFloat = 1.0) { let red = CGFloat((hexString & 0xFF0000) >> 16) / 255.0 let green = CGFloat((hexString & 0x00FF00) >> 8 ) / 255.0 let blue = CGFloat((hexString & 0x0000FF) ) / 255.0 self.init(red: red, green: green, blue: blue, alpha: alpha) } } extension UITableViewDataSource { // return total rows in the tableView func totalRows(tableView: UITableView) -> Int { let totalSections = self.numberOfSectionsInTableView!(tableView) ?? 1 var section = 0, total = 0 while section < totalSections { total += self.tableView(tableView, numberOfRowsInSection: section) section++ } return total } } 
  1. 上一篇文章:
  2. 下一篇文章:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © IOS教程網 All Rights Reserved