您的位置:首页 > 移动开发 > Swift

每个ios开发者都应该知道Top 10 Swift三方库

2017-05-02 17:31 489 查看


1.Alamofire

当你想要抽象简化App中的网络请求时,Alamofire是你需要的,Alamofire是一个Http网络请求库,构建在NSURLSession和基础URL加载系统之上,它用简单优雅的接口很好的封装了网络请求。
// Making a GET request
Alamofire.request(.GET, "https://httpbin.org/get", parameters: ["foo": "bar"])
.responseJSON { response in
print(response.request)  // original URL request
print(response.response) // URL response
print(response.data)     // server data
print(response.result)   // result of response serialization

if let JSON = response.result.value {
print("JSON: \(JSON)")
}
}


2.SwiftyJSON

Swift的Explicit types(显示类型)可以确保我们不会在代码中犯错和出现bug。但是有时处理起来还是比较麻烦,特别是和JSON打交道的时候。幸运的是,SwiftyJSON提供了可读性更好的方式帮我们处理JSON数据。还提供了可选的自动解析!
// Typical JSON handling

if let statusesArray = try? NSJSONSerialization.JSONObjectWithData(data, options: .AllowFragments) as? [[String: AnyObject]],
let user = statusesArray[0]["user"] as? [String: AnyObject],
let username = user["name"] as? String {
// Finally we got the username
}

// With SwiftyJSON

let json = JSON(data: dataFromNetworking)
if let userName = json[0]["user"]["name"].string {
//Now you got your value
}


SwiftyJson也可以很好的和Alamofire配合使用。
Alamofire.request(.GET, url).validate().responseJSON { response in
switch response.result {
case .Success:
if let value = response.result.value {
let json = JSON(value)
print("JSON: \(json)")
}
case .Failure(let error):
print(error)
}
}


3.ObjectMapper

如果你写过一个通过API获取信息的app,你可能需要花大量时间写代码把你的响应结果映射为你的object。ObjectMapper可以帮你把JSON格式响应结果转换成你的model对象,反之亦然。换句话说,它帮你把JSON映射成对象,也可以把对象转换成JSON。嵌套的对象也支持。
// Temperature class that conforms to Mappable protocol

struct Temperature: Mappable {
var celsius: Double?
var fahrenheit: Double?

init?(_ map: Map) {

}

mutating func mapping(map: Map) {
celsius      map["celsius"]
fahrenheit   map["fahrenheit"]
}
}


AlamofireObjectMapper也值得提一下,一个Alamofire的扩展使用ObjectMapper将JSON响应数据转换成swift对象。


4.Quick

Quick是一个行为驱动(BDD)开发框架,它的灵感来自于 

RSpec,Specta, 和Ginkgo。配合Nimble一起使用,Nimble是一个测试匹配框架。
// Documentation directory spec

class TableOfContentsSpec: QuickSpec {
override func spec() {
describe("the 'Documentation' directory") {
it("has everything you need to get started") {
let sections = Directory("Documentation").sections
expect(sections).to(contain("Organized Tests with Quick Examples and Example Groups"))
expect(sections).to(contain("Installing Quick"))
}

context("if it doesn't have what you're looking for") {
it("needs to be updated") {
let you = You(awesome: true)
expect{you.submittedAnIssue}.toEventually(beTruthy())
}
}
}
}
}


5.Eureka

Eureka可以帮你简单优雅的实现动态table-view表单。它由rows,sections和forms组成。如果你的app包含大量表单,Eureka可以真正帮你节省时间。
// Creating a form

class CustomCellsController : FormViewController {

override func viewDidLoad() {
super.viewDidLoad()
form +++ Section("Custom cells")
<<< WeekDayRow(){
$0.value = [.Monday, .Wednesday, .Friday]
}
<<< TextFloatLabelRow() {
$0.title = "Float Label Row, type something to see.."
}
}
}


6.RxSwift

RxSwift是一个基于Swift的的函数式响应编程框架。更具体点,RxSwift是是Rx的一个Swift语言版本(还有Java版本RxJava,js->RxJs)它的目标是让异步和事件数据流操作更简单。KVO
observing, async operations and delegates are all unified under abstraction of sequence(还没来的及学习掩面哭),如果你已经使用过ReactiveCocoa,你接受起来会比较简单(都是函数式编程思想)
// Combine first and last name sequences, and bind the resulting string to label

combineLatest(firstName.rx_text, lastName.rx_text) { $0 + " " + $1 }
.map { "Greeting \($0)" }
.bindTo(greetingLabel.rx_text)


7.SnapKit

SnapKit是一个用少量代码写出不丢可读性auto layout的AutoLayout库。
// Resizing and centering subview in its superview

let box = UIView()
let container = UIView()

container.addSubview(box)

box.snp_makeConstraints { (make)
Void in
make.size.equalTo(50)
make.center.equalTo(container)
}


8.Spring

spring是一个可以帮你用代码或者直接在Storybard创建动画的动画库,在Storyboard你可以用runtime(通过设置IBInspectable属性)来创建动画,spring已经成长为一个全面发展的动画库
支持很多已经存在的动画。
// Usage with code

layer.animation = "wobble"
layer.animate()


9.Kingfisher

Kingfisher是一个轻量的下载和缓存网络图片库。下载和缓存是异步进行操作,已经下载好的图片会缓存在内存和本地,极大得提高app的体验。
// Basic example of downloading and caching an image

imageView.kf_setImageWithURL(NSURL(string: "http://your_image_url.png")!)


10.CoreStore

CoreStore是一个基于Core Data的封装库。它的目标是安全优雅和Core Data进行交互。CoreStore的API提供了常用的有效的方法让你和你的数据库进行交互。
// Storing a person entity

CoreStore.beginAsynchronous { (transaction)
Void in
let person = transaction.create(Into(MyPersonEntity))
person.name = "John Smith"
person.age = 42

transaction.commit { (result)
Void in
switch result {
case .Success(let hasChanges): print("success!")
case .Failure(let error): print(error)
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: