Swift 常用開源項目說明

這部分內(nèi)容其實已經(jīng)很早就有人總結(jié)過,而且內(nèi)容相當豐富。附上 github 地址:

awesome-swift

其實這個系列的還有很多,極大的方便了開發(fā)者尋找自己需要的基礎(chǔ)控件和工具。

本著記錄和分享的態(tài)度,已下對本人覺得有必要深入了解,并可以使用在 swift 上的開源項目進行羅列。

Dollar

github 地址: Dollar
推薦理由:操作數(shù)組的神器
簡介:正如推薦的中所說,Dollar 是一個用來處理數(shù)組的工具集。

Dollar 很強大,她優(yōu)雅的為我們提供了解決數(shù)組,字典等對象的基本操作。比如將數(shù)組劃分成若干個子集,如果我們自己動手做,那少不了for循環(huán)加各種判斷。但使用 Dollar 一行代碼就能搞定。具體接口可以去 github 上查閱。

主要包括以下中對象的操作

SwiftyJSON

github 地址:SwiftyJSON
推薦理由: 用過,因為是swift編寫的所以性能和理解上不存在問題
簡介:是一個使用簡單的處理 json 格式數(shù)據(jù)的工具

詳細的介紹和使用方法可參看 SwiftyJSON DOC 。這里多說一句,SwiftyJSON 的主要目的簡化 JSON 數(shù)據(jù)的解析過程。但建模型的時候還是需要依次獲取 JSON 里的數(shù)據(jù)賦值給模型。當 JSON 數(shù)據(jù)參數(shù)很多的時候,編寫起來會費勁。所以推薦與 ObjectMapper 一起使用。

ObjectMapper

github 地址:ObjectMapper
推薦理由: 用過,很方便而且直觀。只需要在 model 中引用 ObjectMapper 用法即可
簡介:讓數(shù)據(jù)模型的構(gòu)造如此簡單

可能你會有疑問,竟然 ObjectMapper 能將 jsonString 直接轉(zhuǎn)換成 model 那為什么還要使用上面的 SwiftyJSON . 這是因為我們的 JSON 數(shù)據(jù)中并不是所有內(nèi)容都是我們需要的,我們可以通過 SwiftyJSON 先獲取我們需要的數(shù)據(jù)塊,然后在通過 ObjectMapper 獲取 model。

Alamofire

github 地址:Alamofire
推薦理由:用過,OC 上廣為流傳的網(wǎng)絡(luò)請求框架 AFNetworking 已經(jīng)有20000+的star,但對于 swift 開發(fā),個人理解還是要用 swift 的框架的工具比較合適。并且她有多個擴展的工具集,配合使用功能還是很強大的。
簡介:無

Alamofire 擴展

  • AlamofireImage -- AlamofireImage is an image component library for Alamofire。(用過,使用簡單,加載性能也還不錯,只是沒有深入了解其緩存數(shù)據(jù)的問題)
  • AlamofireNetworkActivityIndicator -- Controls the visibility of the network activity indicator on iOS using Alamofire
  • AlamofireObjectMapper -- An extension to Alamofire which automatically converts JSON response data into swift objects using ObjectMapper.(個人覺得不是很好用,除非網(wǎng)絡(luò)返回的 JSON 數(shù)據(jù)格式單一,不需要任何截取操作)

DEMO:

// HttpManager.swift
// 使用Alamfire網(wǎng)絡(luò)請求
// success: (json: JSON)->Void, errors: ()->Void 這兩個閉包用來處理成功和錯誤的操作
// url:訪問地址;parameters;參數(shù)
func requestForGetWithParameters(url: String, parameters params: [String:String], success: (json: JSON)->Void, errors: ()->Void) {
        
        // 使用 Alamofire 進行網(wǎng)絡(luò)數(shù)據(jù)請求
        Alamofire.request(.GET, url, parameters: params).responseJSON {response in
            switch response.result {
            case .Success:
                if let value = response.result.value {
                    let json = JSON(value)
                    NSLog("JSON: \(json["result"])")
                    guard let result = json["result"].string where result == "SUCCESS" else {
                        errors()
                        return
                    }
                    success(json: json)
                }
                break
            case .Failure(let error):
                NSLog("error,failure: \(error)")
                errors()
                break
            }
        }
    }
    
  
 // ViewController.swift 
 // 獲取JSON數(shù)據(jù),截取需要的數(shù)據(jù),生成model
 func onLoadMore() {
        HttpManager.sharedInstance.requestForGetWithPageIndex(
            URL_HOME_ONE,
            pageIndex: pageIndex,
            success: { json in
                // 這里很重要,是用來SwiftyJSON和ObjectMapper生成model
                let home = Mapper<Home>().map(json["hpEntity"].rawValue)
                self.apps.append(home!)
                HomePageViewCell.layoutHeight(home!) //計算cell內(nèi)容高度
                self.pageIndex++
                self.htv!.tableView?.reloadData()
            },
            error: {
                ProgressHUD.showWarningWithStatus("沒有更多內(nèi)容可加載!")
                // 滑動tableview到最后一個cell
                self.htv!.tableView?.selectRowAtIndexPath(NSIndexPath(forRow: 9, inSection: 0), animated: false, scrollPosition: UITableViewScrollPosition.Bottom)
        })
    }

就此,網(wǎng)絡(luò)獲取的請求、json 數(shù)據(jù)處理,model生成就完成了。其實還是挺方便的。

SnapKit

github 地址:SnapKit
簡介:SnapKit is a DSL to make Auto Layout easy。
doc:http://snapkit.io/docs/

因為沒有詳細研究過,所以不好多說。只簡單談一談對Auto Layout的理解。Auto Layout 是 Apple 專門用來配合 IB(Inerface Builder) 解決多拼適配問題的(這樣說當然也有不合適的地方)。通過 IB 設(shè)置控件之間的約束,使控件在不同設(shè)備上相對距離和大小一致。然而不管是通過 IB 還是代碼實現(xiàn)這些約束,都不是一件容易的事情。尤其代碼實現(xiàn),對經(jīng)驗不足的人來說,簡直是地獄。SnapKit 則對代碼實現(xiàn)約束做了封裝,對布局的理解簡單化,也更直觀。
SnapKit 雖然是利器,但用不好就會變兇器。試想一下如果布局復雜,那用代碼實現(xiàn)會很恐怖??催^很多blog后,我有新的理解。那就是對布局模塊化,簡單化。盡量使用 IB 進行布局,對復雜的布局可以嘗試 Container View 進行分割。SnapKit 則是用來微調(diào)和解決大視圖模塊之間無法用 IB 產(chǎn)生約束的情況。

Demo:

import SnapKit

class MyViewController: UIViewController {

    lazy var box = UIView()

    override func viewDidLoad() {
        super.viewDidLoad()

        self.view.addSubview(box)
        box.snp_makeConstraints { (make) -> Void in
           make.width.height.equalTo(50)
           make.center.equalTo(self.view)
        }
    }
}

Kingfisher

github 地址:Kingfisher
介紹:A lightweight and pure Swift implemented library for downloading and caching image from the web. 值得注意的是,他有良好的 Cache management。她是借鑒 SDWebImage 這個由 OC 編寫的10000+star 圖片加載框架。
推薦理由:比AlamofireImage強大,有更好的 cache management 方案。

Kingfisher 和 AlamofireImag 該如何選擇?其實這是一個仁者見仁智者見智的問題。但說到功能強大當然是首選 Kingfisher。如果是簡單圖片展示不需要對圖片進行過多操作 和 cache , AlamofireImage 也是能解決問題。

以下引用 Kingfisher Doc:

Features

  • Everything in Kingfisher is asynchronous, not only downloading, but also caching. That means you never need to worry about blocking your UI thread.
  • Multiple-layer cache. Downloaded images will be cached in both memory and disk. So there is no need to download again, this could boost your app's perceptual speed dramatically.
  • Cache management. You can set the max duration or size the cache takes. From this, the cache will be cleaned automatically to prevent taking too many resources.
  • Modern framework. Kingfisher uses NSURLSession and the latest technology of GCD, which makes it a strong and swift framework. It also provides you easy APIs to use.
  • Cancelable processing task. You can cancel the downloading process if it is not needed anymore.
  • Prefetching. You can prefetch and cache the images which might soon appear in the page. It will bring your users great experience.
  • Independent components. You can use the downloader or caching system separately. Or even create your own cache based on Kingfisher's code.
  • Options to decompress the image in background before rendering it, which could improve the UI performance.
  • Categories over UIImageView, NSImage and UIButton for setting image from an URL directly. Use the same code across all Apple platforms.
  • Support GIF seamlessly. You could just download and set your GIF images as the same as you do for PNG/JPEG format.

HanekeSwift

github 地址:HanekeSwift
簡介:A lightweight generic cache for iOS written in Swift with extra love for images

DEMO:

let cache = Cache<JSON>(name: "github")
let URL = NSURL(string: "https://api.github.com/users/haneke")!

cache.fetch(URL: URL).onSuccess { JSON in
    print(JSON.dictionary?["bio"])
}

以下引用 HanekeSwift DOC:

  • Generic cache with out-of-the-box support for UIImage, NSData, JSON and String
  • First-level memory cache using NSCache
  • Second-level LRU disk cache using the file system
  • Asynchronous fetching of original values from network or disk
  • All disk access is performed in background
  • Thread-safe
  • Automatic cache eviction on memory warnings or disk capacity reached
  • Comprehensive unit tests
  • Extensible by defining custom formats, supporting additional types or implementing custom fetchers
  • Zero-config UIImageView and UIButton extensions to use the cache, optimized for UITableView and UICollectionView cell reuse
  • Background image resizing and decompression

可能你會問,HanekeSwift 和 Kingfisher 都是簡化異步處理網(wǎng)絡(luò)數(shù)據(jù)和圖片的那該如何選擇它們呢?首先從 star 數(shù)上看 HanekeSwift 3000+, Kingfisher 4000+ 其實差不多,畢竟到這個數(shù)量級看的已經(jīng)不止是 star 數(shù)量,更多的是項目的活躍度。Kingfisher 更專注于 image 的加載和緩存,而 HanekeSwift 對 UIImage、NSData、JSON、String 都提供了很好的支持。所以如果是對多種數(shù)據(jù)希望通過統(tǒng)一的解決方案進行緩存管理, HanekeSwift 貌似會更合適。相反對 image 有較高的緩存管理要求,Kingfisher 則會更推薦。

TransitionTreasury

github地址:TransitionTreasury
簡介:A viewController transition framework in Swift。我是被她絢麗的切換方式和簡單的使用方法而迷倒。
使用說明:詳細 Wiki

ios ViewController 的切換可以通過代碼實現(xiàn),也可以通過在 IB 中給ViewController 鏈接 Segue 來實現(xiàn)。兩種方式各有優(yōu)劣,也是根據(jù)個人習慣和實際需要來選擇。TransitionTreasury 是通過代碼來管理 ViewController 的跳轉(zhuǎn),使用起來比較簡單。

作者提供的例子:

/// FirstViewController.swift
class FirstViewController: UIViewController {

    func push() {
        let vc = SecondViewController()
        navigationController?.tr_pushViewController(vc, method: TRPushTransitionMethod.Fade, completion: {
                print("Push finish")
            })
    }
}

/// SecondViewController.swift
class SecondViewController: UIViewController, NavgationTransitionable {

    var tr_pushTransition: TRNavgationTransitionDelegate?

    func pop() {
        tr_popViewController()
    }
}

而對于 IB 中鏈接 Segue 來控制跳轉(zhuǎn),被認為是推薦的方式。比較好的庫有 IBAnimatable ,該庫的作者推崇 IB 的使用,對繪制界面能少用代碼盡量不用。

CryptoSwift

github地址:CryptoSwift
簡介:CryptoSwift is a growing collection of standard and secure cryptographic algorithms implemented in Swift

常見的加解密過程都有,如果使用 Swift 作為開發(fā)語言,這是一個不可錯過的庫。

SwiftMessages

github 地址: SwiftMessages
簡介:一個 Swift 編寫的 Toast 控件。支持多種樣式,當然也可以自定view。很強大。

to be continue...

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 15,383評論 4 61
  • 01. 手指輕觸手機屏幕,給閨蜜發(fā)出信息:“你在干嘛?”一個小時后,手機屏幕終于出現(xiàn)信息:“我在lol呢。臥槽,剛...
    頑抗的小黑女閱讀 796評論 2 2
  • 朋友雪和男朋友分手了,我點頭,絲毫不覺得驚訝。 結(jié)果,大晚上,她拉著我回憶過去,我只能暗暗為我的生物鐘默哀,這明明...
    三日初醒閱讀 841評論 2 4
  • Outlook郵件模板 打開模板會自動添加簽名,保存為模板前切記刪除簽名。 可以把常用的郵件模板打開后固定在任務(wù)欄列表:
    林萬程閱讀 2,934評論 0 2

友情鏈接更多精彩內(nèi)容