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

来源:互联网 发布:通过网络社会治理案例 编辑:程序博客网 时间:2024/05/01 11:25

原文:Top 10 iOS Swift libraries every iOS developer should know about

swift每天都在变的越来越流行。如果你正开始一个新项目,你有机会决定使用swift。为了你转变(从ios转swift)更容易和节省你造轮子的时间,下面是我们认为每个ios开发者都应该知道的10个三方库。

就像我们在Top 5 iOS libraries every iOS developer should know about这篇文章提到的一样,Github和Bitbucker是发现开源库的好地方,像CocoaPods和Carthage这样的工具可以帮你快速安装和管理你项目中的三方库,让你管理依赖更简单。

swift

1.Alamofire

当你想要抽象简化App中的网络请求时,Alamofire是你需要的,Alamofire是一个Http网络请求库,构建在NSURLSession和基础URL加载系统之上,它用简单优雅的接口很好的封装了网络请求。

// Making a GET requestAlamofire.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 handlingif 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 SwiftyJSONlet 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 protocolstruct 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 specclass 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 formclass 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 labelcombineLatest(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 superviewlet 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 codelayer.animation = "wobble"layer.animate()

9.Kingfisher

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

// Basic example of downloading and caching an imageimageView.kf_setImageWithURL(NSURL(string: "http://your_image_url.png")!)

10.CoreStore

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

// Storing a person entityCoreStore.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)        }    }}

你的看法呢

你不认同?一些其他的三方库应该出现在这个列表?给我们留言让我们知道你的想法!

0 0
原创粉丝点击