IOS --- Summary of common global functions and extension in Swift (continuous update)

Source: Internet
Author: User

IOS --- Summary of common global functions and extension in Swift (continuous update)

Sort out the common Swift global functions and extension (because macro definitions cannot be used in Swift) in iOS development as follows.
For a summary of Objective-C's macro definition, refer to the frequently-used macro definition in Objective-C in the blog iOS-Summary (continuously updated ).

////  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    }}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.