Swift Constant File

作者: Jiubao | 来源:发表于2016-03-28 18:11 被阅读103次

    //Update 0729

    `dlog` 更新:[http://www.jianshu.com/p/e65d7c661741]

    整理下 Swift 中常用的 Constant.swift 文件

    import UIKit

    extension String {

    var floatValue: Float {

    return (self as NSString).floatValue

    }

    }

    extension String {

    func stringByAppendingPathComponent(path: String) -> String {

    let nsSt = self as NSString

    return nsSt.stringByAppendingPathComponent(path)

    }

    }

    //MARK: Screen

    let SCREEN_BOUNDS = UIScreen.mainScreen().bounds

    let SCREEN_SCALE = UIScreen.mainScreen().scale

    let SCREEN_WIDTH = UIScreen.mainScreen().bounds.size.width

    let SCREEN_HEIGHT = UIScreen.mainScreen().bounds.size.height

    let SCREEN_MAX_LENGTH = max(SCREEN_WIDTH, SCREEN_HEIGHT)

    let SCREEN_MIN_LENGTH = min(SCREEN_WIDTH, SCREEN_HEIGHT)

    //MARK: Device

    let IS_IPAD = UIDevice.currentDevice().userInterfaceIdiom == UIUserInterfaceIdiom.Pad

    let IS_IPHONE = UIDevice.currentDevice().userInterfaceIdiom == UIUserInterfaceIdiom.Phone

    let IS_IPHONE_4_OR_LESS = (IS_IPHONE && SCREEN_MAX_LENGTH < 568.0)

    let IS_IPHONE_5 = (IS_IPHONE && SCREEN_MAX_LENGTH == 568.0)

    let IS_IPHONE_6 = (IS_IPHONE && SCREEN_MAX_LENGTH == 667.0)

    let IS_IPHONE_6P = (IS_IPHONE && SCREEN_MAX_LENGTH == 736.0)

    //MARK: System Version

    let IS_OS_7_OR_LATER = (UIDevice.currentDevice().systemVersion as String).floatValue >= 7.0

    let IS_OS_8_OR_LATER = (UIDevice.currentDevice().systemVersion as String).floatValue >= 8.0

    let IS_OS_9_OR_LATER = (UIDevice.currentDevice().systemVersion as String).floatValue >= 9.0

    //MARK: Macros

    /**

    dLog macros to abbreviate NSLog.

    Use like this:

    dLog(“Log this!”)

    **/

    #if DEBUG

    func dLog(@autoclosure message:  () -> String, filename: String = __FILE__, function: String = __FUNCTION__, line: Int = __LINE__) {

    NSLog(“[\(filename.stringByAppendingPathComponent):\(line)] \(function) – %@”, message())

    }

    #else

    func dLog(@autoclosure message:  () -> String, filename: String = __FILE__, function: String = __FUNCTION__, line: Int = __LINE__) {

    }

    #endif

    整理的时候发现,下面的两行代码如果一起放到 Constant.swift  中会导致编辑失败

    let IS_IPAD = UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiom.Pad

    let IS_OS_7_OR_LATER = (UIDevice.currentDevice().systemVersion as String).floatValue >= 7.0

    错误提示:

    IR generation failure: program too clever: variable collides with existing symbol OBJC_CLASS_$_UIDevice

    解决方法是将 UI_USER_INTERFACE_IDIOM() 方法替换为

    let IS_IPAD = UIDevice.currentDevice().userInterfaceIdiom == UIUserInterfaceIdiom.Pad

    因为苹果关于 UI_USER_INTERFACE_IDIOM() 的文档有描述

    The UI_USER_INTERFACE_IDIOM() function is provided for use when deploying to a version of the iOS less than 3.2. If the earliest version of iPhone/iOS that you will be deploying for is 3.2 or greater, you may use -[UIDevice userInterfaceIdiom] directly.

    另外,dlog 生效需要在 Build Settings – Swift Compiler – Custom Flags – Other Swift Flags 中,Debug 一栏,增加

    -D DEBUG

    才可以生效

    以上

    相关文章

      网友评论

        本文标题:Swift Constant File

        本文链接:https://www.haomeiwen.com/subject/txgjlttx.html