swift常用三方库简单介绍,详细用法可见github上库介绍
- 'Alamofire' 网络库
- 'SnapKit' 布局约束
- 'SwiftyJSON' json解析
1. 'Alamofire' 网络库
//登录 post
class func login(_ phone:NSString, _ password:NSString, _ success:@escaping VoidClosure){
let url = hostUrl + "uaa/oauth/token"
let loginRequest = ["username":phone,"password":password,"grant_type":"password"]
let headers = ["client_secret":"123456","Authorization":"Basic ZGVmYXVsdDoxMjM0NTY="]
Alamofire.request(url, method: .post, parameters: loginRequest, encoding: URLEncoding.default, headers: headers).validate().responseJSON { response in
switch response.result {
case .success:
do {
if let Json = response.result.value{
print("Json:\(Json)")
}
success ()
}
case .failure(let error):
print(error)
}
}
}
//获取用户信息 get
class func getUserInfo(success:@escaping AnyClosure){
let headers = ["client_id":"default","Content-Type":"application/json","app_id":"prop-pro-ios","Authorization":UserDefaults.standard.object(forKey: Authorization as String)]
Alamofire.request(hostUrl + "uaa/users/profile", method: .get, parameters: nil, encoding: URLEncoding.default, headers: headers as? HTTPHeaders).validate().responseJSON { response in
switch response.result {
case .success(let value):
do{
let jsonData = JSON(value)
SwiftUser.shareInstance = SwiftUser(json: jsonData)
success (jsonData as AnyObject)
}
case .failure(let error):
print(error)
}
}
}
}
2.'SnapKit' 布局约束
//这个其实跟masonry非常非常类似,用的是.语法,“snp.makeConstraints”相当于ma s_makeConstraints,"$0"相当于make
versionLabel.snp.makeConstraints{
$0.left.equalTo(50)
$0.centerX.equalToSuperview()
$0.top.equalTo(30)
}
logoImgView.snp.makeConstraints {
$0.left.equalToSuperview().offset(150)
$0.width.height.equalTo(80)
$0.centerX.equalToSuperview()
$0.top.equalToSuperview().offset(100)
}
nameTextField.snp.makeConstraints {
$0.centerX.equalToSuperview()
$0.top.equalTo(logoImgView.snp_bottom).offset(30)
$0.size.equalTo(CGSize(width: 200, height: 40))
}
passwordTextField.snp.makeConstraints {
$0.centerX.equalToSuperview()
$0.top.equalTo(nameTextField.snp_bottom).offset(30)
$0.width.height.equalTo(nameTextField)
}
loginBtn.snp.makeConstraints{
$0.centerX.equalToSuperview()
$0.top.equalTo(passwordTextField.snp_bottom).offset(10)
$0.size.equalTo(CGSize(width: 200, height: 60))
}
infoTextView.snp.makeConstraints{
$0.centerX.equalToSuperview()
$0.top.equalTo(loginBtn.snp_bottom).offset(10)
$0.left.equalTo(30)
$0.height.equalTo(100)
}
swiftSwitch.snp.makeConstraints{
$0.top.equalTo(infoTextView.snp_bottom).offset(10)
$0.left.equalTo(30)
$0.size.equalTo(CGSize(width: 60, height: 30))
}
stepper.snp.makeConstraints{
$0.centerY.equalTo(swiftSwitch)
$0.left.equalTo(swiftSwitch.snp_right).offset(50)
$0.size.equalTo(CGSize(width: 100, height: 30))
}
slider.snp.makeConstraints{
$0.top.equalTo(swiftSwitch.snp_bottom).offset(10)
$0.centerX.equalToSuperview()
$0.size.equalTo(CGSize(width: 210, height: 10))
}
progress.snp.makeConstraints{
$0.top.equalTo(slider.snp_bottom).offset(10)
$0.centerX.equalToSuperview()
$0.size.equalTo(CGSize(width: 210, height: 3))
}
segment.snp.makeConstraints{
$0.top.equalTo(progress.snp_bottom).offset(10)
$0.centerX.equalToSuperview()
$0.size.equalTo(CGSize(width: 210, height: 35))
}
3.'SwiftyJSON' json解析
import UIKit
import SwiftyJSON
class SwiftUser: NSObject {
var phoneNumber,avatar,name,birthdate,gender,id :String?
var loginProviders : Array<NSString>?
static var shareInstance = SwiftUser(json: "")
//类需要有一个init构造函数
init(json: JSON) {
phoneNumber = json["phoneNumber"].stringValue
avatar = json["avatar"].stringValue
name = json["name"].stringValue
birthdate = json["birthdate"].stringValue
gender = json["gender"].stringValue
id = json["id"].stringValue
loginProviders = json["loginProviders"].arrayObject as? Array<NSString>
let aa = 10 + SCREEN_W
print(aa)
}
}
Alamofire.request("http://www.xxx.host", method: .get, parameters: nil, encoding: URLEncoding.default, headers: headers as? HTTPHeaders).validate().responseJSON { response in
switch response.result {
case .success(let value):
do{
let jsonData = JSON(value)
SwiftUser.shareInstance = SwiftUser(json: jsonData)
success (jsonData as AnyObject)
}
case .failure(let error):
print(error)
}
}