swift常用的宏

前端之家收集整理的这篇文章主要介绍了swift常用的宏前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

在以前的C 和 OC 中经常使用宏常亮,简化代码。swift 可以直接建一个swift 文件,写在里面的常亮可以全局调用

<span style="font-size:18px;">// 当前系统版本 
let  version = (UIDevice.currentDevice().systemVersion as NSString).floatValue

// 屏幕宽度
let screenHeight = UIScreen.mainScreen().bounds.height
// 屏幕高度
let screenWidth = UIScreen.mainScreen().bounds.width
// 默认图片
let defaultImg = UIImage(named: "photo_define")
// NSUserDefault
let userDefault = NSUserDefaults.standardUserDefaults()
// 通知中心
let notice = NSNotificationCenter.defaultCenter()
//判断是不是plus
let currentModeSize = UIScreen.mainScreen().currentMode?.size
let isPlus = UIScreen.instancesRespondToSelector("currentMode") ? CGSizeEqualToSize(CGSizeMake(1242,2208),currentModeSize!) : false
//判断字符串是否为空
func trimString(#str:String)->String{
    var nowStr = str.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceCharacterSet())
    return nowStr
}

//去除空格和回车
func trimLineString(#str:String)->String{
    var nowStr = str.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet())
    return nowStr
}
//根据键盘监控  获取键盘高度
func getKeyBoardHeight(#aNotification:NSNotification)->CGFloat{
    var uInfo   = aNotification.userInfo as NSDictionary!
    let avalue = uInfo["UIKeyboardFrameEndUserInfoKey"] as NSValue
    let keyrect : CGRect = avalue.CGRectValue()
    let keyheight : CGFloat = keyrect.size.height;
    return keyheight
}
//获取目录下存储的json文件并解析为集合
func getNativeJson(#filename : String,#fileext : String)->AnyObject{
    let pathsBun = NSBundle.mainBundle()
    let paths = pathsBun.pathForResource(filename,ofType : fileext)
    var errors:NSError?
    var content : NSData = NSData(contentsOfFile: paths!,options : .DataReadingMappedIfSafe,error: nil)!
    var returneddata: AnyObject?  = NSJSONSerialization.JSONObjectWithData(content as NSData,options:NSJSONReadingOptions.MutableContainers,error:&errors)
        return returneddata!
}
//消息提醒
func showAlertView(#title:String,#message:String)
{
    var alert = UIAlertView()
    alert.title = title
    alert.message = message
    alert.addButtonWithTitle("好")
    alert.show()
}
//获取本地存储数据
func get_userDefaults(#key : String)->AnyObject?{
  
    var saveStr : AnyObject! = userDefault.objectForKey(key)
    saveStr = (saveStr == nil) ? "" : saveStr
    return saveStr
}
//存储数据
func save_userDefaults(#key : String,#value:AnyObject?){
    
    userDefault.setObject(value!,forKey:key)
}
//字符串转数组
func stringToArray(#str:String)->NSArray{
    var dataArray:[String] = []
    for items in str{
        dataArray.append("\(items)")
    }
    return dataArray
}</span>
原文链接:https://www.f2er.com/swift/327510.html

猜你在找的Swift相关文章