一、GCD
延时调用
HUD_DISMISS_TIME
为延时执行的时间
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + HUD_DISMISS_TIME, execute: {
//在这里做需要延时的操作
})
二、GCD
队列
self.group = DispatchGroup.init()
let holiday1 = DispatchQueue(label: "holiday")
holiday1.async(group: group) {
//操作1
}
let holiday2 = DispatchQueue(label: "holiday")
holiday2.async(group: group) {
//操作2
}
self.group?.notify(queue: DispatchQueue.main) {
//完成上面的操作,通知开始下一步操作
}
三、判断一个对象是否为空
if dicData is NSNull {
//
}
四、日期格式化
//MARK:日期格式化-日期转字符串
func dateFormatter(_ date:Date ,formatter:String) ->String{
let dateformatter = DateFormatter()
dateformatter.dateFormat = formatter
let d = dateformatter.string(from: date)
return d
}
//MARK:日期格式化-日期转日期
func dateFormatterForDate(_ date:Date,formatter:String) -> Date {
let dateformatter = DateFormatter()
dateformatter.dateFormat = formatter
let datee = date
return datee
}
//MARK:日期格式化-字符串转日期
func stringToDate(_ string:String,formatter:String) ->Date{
let dateformatter = DateFormatter()
dateformatter.dateFormat = formatter
let date:Date = dateformatter.date(from: string)!
return date
}
五 、定义闭包(block块)类型时,当参数类型为Dictionary,如下,
typealias RequestSuccess = (_ success:Dictionary) ->Void
会出现错误提示:
Reference to generic type 'Dictionary' requires arguments in <...>
Insert '<<#Key: Hashable#>, Any>'
解决办法:
修改为typealias RequestSuccess = (_ success:Dictionary<AnyHashable, Any>) ->Void
即可。
原因
AnyHashable
是调和objc
和Swift
的产物
典型的场景是,objc
下无泛型的NSDictionary
到了Swift
下,会变成什么?(典型例子是苹果来的推送消息)
[Any: Any]
,这肯定不对,key
必须有hash
值,
[AnyObject: Any]
,这也不对,同样的AnyObject
也未必有hash值
大一统一的理论来了,那就是AnyHashable
这个Struct
[AnyHashable: Any]
就是NSDictionary
在Swift
下的形态。
六、Swift
项目中使用AFNetworking
/*
说明:基础的网络请求设置
*/
import UIKit
import AFNetworking
let ipString = "http://114.114.114.114:74114"
class BaseRequest: NSObject {
public static func requestManager() ->AFHTTPSessionManager{
let manager:AFHTTPSessionManager = AFHTTPSessionManager.init()
manager.requestSerializer = AFJSONRequestSerializer()//请求
//manager.responseSerializer = AFJSONResponseSerializer()//可以根据服务器类型来选择这个或者下面的
manager.responseSerializer = AFHTTPResponseSerializer()//响应
//TODO:安全连接
let securityPolicy = AFSecurityPolicy.init()
securityPolicy.allowInvalidCertificates = true
manager.securityPolicy = securityPolicy
//TODO:请求类型
let netSet:Set<String> = ["application/json",
"text/json",
"text/javascript",
"application/x-json",
"text/html",
"text/plain",
"image/jpg",
"application/json; charset=utf-8",
"multipart/form-data"]
manager.responseSerializer.acceptableContentTypes = netSet
//TODO:设置超时
manager.requestSerializer.willChangeValue(forKey: "timeoutInterval")
manager.requestSerializer.timeoutInterval = 10.0
manager.requestSerializer.willChangeValue(forKey: "timeoutInterval")
return manager
}
}
import Foundation
import UIKit
import AFNetworking
//MARK: 闭包(block)类型
typealias RequestSuccess = (_ success:NSDictionary) ->Void
typealias RequestFail = (_ fail:Error) ->Void
typealias CancelRequest = () ->Void
/*
说明:登录模块的请求
*/
import UIKit
import AFNetworking
class LoginModuleRequest: NSObject {
public static func requestForUserLogin(_ userName:String,passWord:String,success:@escaping RequestSuccess,fail:@escaping RequestFail) -> URLSessionDataTask{
let manager:AFHTTPSessionManager = BaseRequest.requestManager()
let param:[String:String] = ["UserName":userName,"Password":passWord]
let urlString:String = String.init(format: "%@/Api/Account/Login", ipString)
let task = manager.post(urlString.addingPercentEncoding(withAllowedCharacters: CharacterSet.urlQueryAllowed)!,
parameters: param,
progress: nil,
success: { (task:URLSessionDataTask!, responseObject:Any!) in
if responseObject != nil{
let dataString = String.init(data: responseObject as! Data, encoding: String.Encoding.utf8)
let dic = RequestTools.dicFromJson(dataString!) as! NSDictionary
success(dic)
}
}) { (task:URLSessionDataTask!, error:Error!) in
if error != nil{ fail(error) }
}
return task!
}
}