iOS --- 總結Swift中常用的全域函數和extension(持續更新中)

來源:互聯網
上載者:User

iOS --- 總結Swift中常用的全域函數和extension(持續更新中)

將iOS開發中常用的Swift全域函數和extension(因Swift中不能使用宏定義)整理如下。
而對於Objective-C,其宏定義總結請參考部落格iOS — 總結Objective-C中常用的宏定義(持續更新中)。

////  Global.swift////  useful global methods and extension for Chris Hu//import Foundationfunc 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    }}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.