swift 基础方法的封装

2017-09-08  本文已影响0人  id_confidence

CTCateory

swift oc 基本类用swift实现

前言

本文主要是针对swift封装基础方法类,方便开发者开发。我们之前在开发OC的时候有大量的基类、工具类。我们也封装了大量的控件,那么随了swift的普及,swift的优势让我感到需要做这么一件事。

github链接:https://github.com/chen1230/CTCateory.git,欢迎给星

CTCateory

 这个基类库会不断丰富,目前只有ColorCategory,DateCategory,StringCategory 三个。

ColorCategory

颜色类:提供了三个方法,讲开发中的颜色转化为我们可用的UIColor

  /**
      /**
     *    method 十六进制数转化  开头 0x
     *    return UIColor  
     *    param  Int
     */
    public  func hexValue(_ hexValue: Int) -> UIColor{
    
    return UIColor.init(colorLiteralRed: Float((((hexValue) & 0xFF0000) >> 16) / 255), green: Float((((hexValue) & 0xFF00) >> 8) / 255), blue: Float((((hexValue) & 0xFF)) / 255) , alpha: 1.0)
    
    }

 /**
     *    method RGB 转化
     *    return UIColor
     *    param  Int
     */
    public func colorRGB(red:float_t,green:float_t,blue:float_t) -> UIColor{
    
     return UIColor.init(colorLiteralRed: red/255.0, green:green/255.0, blue: blue/255.0 , alpha: 1.0)
    
    }

 /**
     *    method RGB 十六进制字符串转化
     *    return UIColor
     *    param  String
     */
    public func colorForHex(_ str:String) ->UIColor {
    
        let range1 = NSRange(location: 0, length: 2)
        let rStr = (str as NSString).substring(with: range1)
        
        let range2 = NSRange(location: 2, length: 2)
        let gStr = (str as NSString).substring(with: range2)
        
        let range3 = NSRange(location: 4, length: 2)
        let bStr = (str as NSString).substring(with: range3)
        
        var r:CUnsignedInt = 0, g:CUnsignedInt = 0, b:CUnsignedInt = 0;
        Scanner(string: rStr).scanHexInt32(&r)
        Scanner(string: gStr).scanHexInt32(&g)
        Scanner(string: bStr).scanHexInt32(&b)
        
       return self.colorRGB(red: float_t(r), green: float_t(g), blue: float_t(b))
        
        
    }


DateCategory

时间类:提供了常规的时间方法,大多数是OC中用过.有取当前时间、转时间戳、时间戳转时间字符串 等等
 /**
     *    method  获取当前时间戳
     *    return  时间戳
     *    param   nil
     *    默认格式  "yyyy-MM-dd"    字符串必须和famate一致
     */
    public func getCurrentTimeInterval() ->TimeInterval? {
        let  dateFormat = DateFormatter()
        dateFormat.dateFormat = "yyyy-MM-dd"
        let time = self.getCurrentDate(famate: nil)
        //设置时区
        let timeZone = TimeZone(abbreviation: "CST") // GMT:Greenwich Mean Time   CST:China Standard Time  CET:Central European Time
        dateFormat.timeZone = timeZone
        let date = dateFormat.date(from: time)
        let dateStamp:TimeInterval = (date?.timeIntervalSince1970)!
        return TimeInterval(dateStamp)
    }

StringCategory

字符串类:主要是方法:

/**
     *    method 获取系统版本号
     *    return String  版本号
     *    param  nil
     */
    public func getCurrentVersion() -> String{
    
       return Bundle.main.infoDictionary!["CFBundleShortVersionString"] as! String
    }

上一篇下一篇

猜你喜欢

热点阅读