// // Const.swift // swift翻译 // // Created by zhongyuan on 2018/3/20. // Copyright © 2018年 zhongyuan. All rights reserved. // import UIKit let kScreenWidth = UIScreen.main.bounds.size.width let kScreenHeight = UIScreen.main.bounds.size.height let Is_Iphone = (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiom.phone) let Is_Iphone_XxS = (Is_Iphone && kScreenWidth == 375.0 && kScreenHeight == 812.0) let Is_Ipone_XRxSMax = (Is_Iphone && kScreenWidth == 414.0 && kScreenHeight == 896.0) let Is_Ipone12_ProMax = (Is_Iphone && kScreenWidth == 428.0 && kScreenHeight == 926.0) let Is_Iphone_6p7p8p = (Is_Iphone && kScreenWidth == 414.0 && kScreenHeight == 736.0) let Is_Iphone_678 = (Is_Iphone && kScreenWidth == 375.0 && kScreenHeight == 667.0) let Is_Iphone_5 = (Is_Iphone && kScreenWidth == 320.0 && kScreenHeight == 568.0) let Is_Iphone_4 = (Is_Iphone && kScreenWidth == 320.0 && kScreenHeight == 480.0) let hasSafeArea = Is_Ipone12_ProMax || Is_Iphone_XxS || Is_Ipone_XRxSMax let NaviHeight = (CGFloat)(hasSafeArea ? 88 : 64) let TabbarHeight = (CGFloat)(hasSafeArea ? 83 : 49) /// 底部安全区域远离高度 let kBottomSafeHeight = (CGFloat)(hasSafeArea ? 34 : 0) /// 顶部安全区域远离高度 let kTopBarSafeHeight = (CGFloat)(hasSafeArea ? 44:0) /// 状态栏高度 let kStatusBarHeight = (CGFloat)(hasSafeArea ? 44:20) /// 获取应用名称和版本号 let app_Name = (Bundle.main.infoDictionary! as NSDictionary) .object(forKey: "CFBundleDisplayName") as! String let app_Version = Bundle.main.infoDictionary!["CFBundleShortVersionString"] as! String /// 输入框 placeholder text color var Color_placeholder_text = UIColorFromHex(rgbValue: 0x515151) /// 导航栏背景色 var Color_Navigation_BarTint = UIColorFromHex(rgbValue: 0x1882D2) /// Tabbar Item normal var Color_BarItem_Normal = UIColorFromHex(rgbValue: 0x797296) /// Tabbar Item Selected var Color_BarItem_Selected = UIColorFromHex(rgbValue: 0x797296) /// Color_CollectionCell_Border var Color_CollectionCell_Border = UIColorFromHex(rgbValue: 0xF0F0F0) /// 空值转"--"、0、0.0 func formatStr(str:Any) ->String{ if let str = str as? Double { return str == 0.0 ? "--" : String(str) } else if let str = str as? String { return str == "null" ? "--" : str } else if let str = str as? Int { return str == 0 ? "--" : String(str) } else{ return "--" } } /// 横竖屏 func setNewOrientation(fullScreen: Bool) { if fullScreen { /// 横屏 let resetOrientationTargert = NSNumber(integerLiteral: UIInterfaceOrientation.unknown.rawValue) UIDevice.current.setValue(resetOrientationTargert, forKey: "orientation") let orientationTarget = NSNumber(integerLiteral: UIInterfaceOrientation.landscapeLeft.rawValue) UIDevice.current.setValue(orientationTarget, forKey: "orientation") } else { /// 竖屏 let resetOrientationTargert = NSNumber(integerLiteral: UIInterfaceOrientation.unknown.rawValue) UIDevice.current.setValue(resetOrientationTargert, forKey: "orientation") let orientationTarget = NSNumber(integerLiteral: UIInterfaceOrientation.portrait.rawValue) UIDevice.current.setValue(orientationTarget, forKey: "orientation") } } /// ColorFromHex /// - Parameters: /// - rgbValue: rgbValue /// - alpha: alpha /// - Returns: UIColor func UIColorFromHex(rgbValue:Int, alpha:CGFloat = 1.0) -> UIColor { return UIColor.init(red: (CGFloat)((Float)((rgbValue & 0xFF0000) >> 16))/255.0, green: ((CGFloat)((rgbValue & 0xFF00) >> 8))/255.0, blue: ((CGFloat)(rgbValue & 0xFF))/255.0 , alpha: alpha) } // MARK: - 数组去重方法 extension Array { /// 该函数的参数filterCall是一个带返回值的闭包,传入模型T,返回一个E类型 func handleFilter(_ filterCall: (Array.Element) -> E) -> [Array.Element] { var temp = [Array.Element]() for model in self { /// 调用filterCall,获得需要用来判断的属性E let identifer = filterCall(model) /// 此处利用map函数 来将model类型数组转换成E类型的数组,以此来判断identifer 是否已经存在,如不存在则将model添加进temp if !temp.compactMap( { filterCall($0) } ).contains(identifer) { temp.append(model) } } return temp } } /// swift字典转模型 /// /// - Parameters: /// - type: 模型类 /// - KeyAndValues: 字典 /// - Returns: 模型对象 /// - Throws: 转换失败 && 解码失败 func jsonModel(_ type: T.Type, with KeyAndValues: [String: Any]) -> T? where T: Decodable { let jsonObject = try? JSONSerialization.data(withJSONObject: KeyAndValues, options: []) return try? JSONDecoder().decode(type, from: jsonObject!) }