- Block语法
public typealias COMPLETE_BLOCK = (AnyObject!, Int32, AnyObject!) -> Void
public typealias PROGRESS_BLOCK = (Int64, Int64) -> Void```
- 使用Block
public func rpcRequestWithURL(url: String!, operationType: String!, requestData: String!, block: COMPLETE_BLOCK!)```
- 使用guard let代替 if else 处理异常情况
guard let videoURLStr = messageModel.param["previewUrl"] as? String,
title = messageModel.param["strTitle"] as? String,
desc = messageModel.param["strDesc"] as? String else {return}```
- 用lazy关键字处理不常用的View等控件,比如弹窗,下拉菜单等等
lazy var previewView:XYTemplatePreviewView = {
let view = NSBundle.mainBundle().loadNibNamed("XYTemplatePreviewView", owner: self, options: nil).last as! XYTemplatePreviewView
return view
}()```
- 更灵活的enum(枚举)以及Swith case语句
enum XYBridgedMethod : String {
case GetLocalThemeList = "getLocalThemeList"
case DownloadTheme = "downloadTheme"
case UseTheme = "useTheme"
case GetLanguage = "getLanguage"
case ShowThemeVideo = "showThemeVideo"
}
enum XYThemeShopUseThemeType {
case ApplyTheme
case CreateNewProject
}```
- 更方便的扩展extension
extension String {
func reverseString() -> String {
// do something if necessary
}
} ```
extension UIColor {
class func clipboardTopBarBGColor() -> UIColor{
switch colorTheme{
case .Default:
return UIColor.color(hex: 0x424242)
}
}
}```
- get set方法重写更为简单
var clipSize:CGSize{
get{
let flowLayout = collectionView.collectionViewLayout as! UICollectionViewFlowLayout
return flowLayout.itemSize
}
set{
}
}```
- 使用willSet与didSet来监听值得属性改变
// 普通属性
var nickName:String! = ""
var lastName:String! = ""
var firstName:String! = ""
// 计算属性
var fullName:String!{
get{
return nickName + "" + lastName + firstName
}
}
//带属性监视器的普通属性
var age:Int = 0
{
//我们需要在age属性变化前做点什么
willSet
{
print("Will set an new value \(newValue) to age")
}
//我们需要在age属性发生变化后,更新一下nickName这个属性
didSet
{
print("age filed changed form \(oldValue) to \(age)")
if age<10
{
nickName = "Little"
}else
{
nickName = "Big"
}
}
}```
- 一句话搞定单例
import Foundation
class SingleOne {
//单例
static let shareSingleOne = SingleOne()
}```
- .#if endif使用
//使用#if DEBUG 需要在Other Swift Flags中,DEBUG下添加-D DEBUG
func printLog(message: T, file: String = #file, method: String = #function, line: Int = #line){
#if DEBUG
print(logString(message, file: file, method: method, line: line), terminator: "\n")
#endif
}```
- Block弱引用 **unowned**关键字
override func addMoreMethod() {
addMethod(methodName: .GetLocalThemeList) {[unowned self] (messageModel) in
let callbackModel = self.getCallbackModel(messageModel)
let localThemeListDict = self.getLocalThemeList()
let themeListJSON = XYH5ContainerTool.JSONStringWithDic(localThemeListDict)
callbackModel.param = ["themes":themeListJSON]
self.sendMessageToWebWithH5MessageModel(callbackModel)
}
}```
GCD的简易封装
子线程或延迟执行
func async(after second:Double? = nil, block:()->Void){
if let sec = second {
dispatch_after(dispatchTimeAfterNow(sec), dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), block)
}else{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), block)
}
}```
- 计算时间
func dispatchTime(second:Double)->Int64{
return (Int64)(second * Double(NSEC_PER_SEC))
}
func dispatchTimeAfterNow(second:Double)->dispatch_time_t{
return dispatch_time(DISPATCH_TIME_NOW, dispatchTime(second))
}```
- 主线程或延迟执行
func asyncMain(after second:Double? = nil, block:()->Void){
if let sec = second {
dispatch_after(dispatchTimeAfterNow(sec), dispatch_get_main_queue(), block)
}else{
dispatch_async(dispatch_get_main_queue(), block)
}
}```
- 子线程或延时执行
func async(after second:Double? = nil, block:()->Void){
if let sec = second {
dispatch_after(dispatchTimeAfterNow(sec), dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), block)
}else{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), block)
}
}```
- 打印log字符串
private func logString(message:T, file:String, method:String, line:Int)->String{
return "\(printLogDateFormatter.stringFromDate(NSDate())) \((file as NSString).lastPathComponent)[\(line)], \(method): \(message)"
}```