Swift 常用开源项目说明

这部分内容其实已经很早就有人总结过,而且内容相当丰富。附上 github 地址:

awesome-swift

其实这个系列的还有很多,极大的方便了开发者寻找自己需要的基础控件和工具。

本着记录和分享的态度,已下对本人觉得有必要深入了解,并可以使用在 swift 上的开源项目进行罗列。

Dollar

github 地址: Dollar
推荐理由:操作数组的神器
简介:正如推荐的中所说,Dollar 是一个用来处理数组的工具集。

Dollar 很强大,她优雅的为我们提供了解决数组,字典等对象的基本操作。比如将数组划分成若干个子集,如果我们自己动手做,那少不了for循环加各种判断。但使用 Dollar 一行代码就能搞定。具体接口可以去 github 上查阅。

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

SwiftyJSON

github 地址:SwiftyJSON
推荐理由: 用过,因为是swift编写的所以性能和理解上不存在问题
简介:是一个使用简单的处理 json 格式数据的工具

详细的介绍和使用方法可参看 SwiftyJSON DOC 。这里多说一句,SwiftyJSON 的主要目的简化 JSON 数据的解析过程。但建模型的时候还是需要依次获取 JSON 里的数据赋值给模型。当 JSON 数据参数很多的时候,编写起来会费劲。所以推荐与 ObjectMapper 一起使用。

ObjectMapper

github 地址:ObjectMapper
推荐理由: 用过,很方便而且直观。只需要在 model 中引用 ObjectMapper 用法即可
简介:让数据模型的构造如此简单

可能你会有疑问,竟然 ObjectMapper 能将 jsonString 直接转换成 model 那为什么还要使用上面的 SwiftyJSON . 这是因为我们的 JSON 数据中并不是所有内容都是我们需要的,我们可以通过 SwiftyJSON 先获取我们需要的数据块,然后在通过 ObjectMapper 获取 model。

Alamofire

github 地址:Alamofire
推荐理由:用过,OC 上广为流传的网络请求框架 AFNetworking 已经有20000+的star,但对于 swift 开发,个人理解还是要用 swift 的框架的工具比较合适。并且她有多个扩展的工具集,配合使用功能还是很强大的。
简介:无

Alamofire 扩展

  • AlamofireImage -- AlamofireImage is an image component library for Alamofire。(用过,使用简单,加载性能也还不错,只是没有深入了解其缓存数据的问题)
  • 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.(个人觉得不是很好用,除非网络返回的 JSON 数据格式单一,不需要任何截取操作)

DEMO:

// HttpManager.swift
// 使用Alamfire网络请求
// success: (json: JSON)->Void, errors: ()->Void 这两个闭包用来处理成功和错误的操作
// url:访问地址;parameters;参数
func requestForGetWithParameters(url: String, parameters params: [String:String], success: (json: JSON)->Void, errors: ()->Void) {
        
        // 使用 Alamofire 进行网络数据请求
        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数据,截取需要的数据,生成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内容高度
                self.pageIndex++
                self.htv!.tableView?.reloadData()
            },
            error: {
                ProgressHUD.showWarningWithStatus("没有更多内容可加载!")
                // 滑动tableview到最后一个cell
                self.htv!.tableView?.selectRowAtIndexPath(NSIndexPath(forRow: 9, inSection: 0), animated: false, scrollPosition: UITableViewScrollPosition.Bottom)
        })
    }

就此,网络获取的请求、json 数据处理,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 设置控件之间的约束,使控件在不同设备上相对距离和大小一致。然而不管是通过 IB 还是代码实现这些约束,都不是一件容易的事情。尤其代码实现,对经验不足的人来说,简直是地狱。SnapKit 则对代码实现约束做了封装,对布局的理解简单化,也更直观。
SnapKit 虽然是利器,但用不好就会变凶器。试想一下如果布局复杂,那用代码实现会很恐怖。看过很多blog后,我有新的理解。那就是对布局模块化,简单化。尽量使用 IB 进行布局,对复杂的布局可以尝试 Container View 进行分割。SnapKit 则是用来微调和解决大视图模块之间无法用 IB 产生约束的情况。

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 都是简化异步处理网络数据和图片的那该如何选择它们呢?首先从 star 数上看 HanekeSwift 3000+, Kingfisher 4000+ 其实差不多,毕竟到这个数量级看的已经不止是 star 数量,更多的是项目的活跃度。Kingfisher 更专注于 image 的加载和缓存,而 HanekeSwift 对 UIImage、NSData、JSON、String 都提供了很好的支持。所以如果是对多种数据希望通过统一的解决方案进行缓存管理, HanekeSwift 貌似会更合适。相反对 image 有较高的缓存管理要求,Kingfisher 则会更推荐。

TransitionTreasury

github地址:TransitionTreasury
简介:A viewController transition framework in Swift。我是被她绚丽的切换方式和简单的使用方法而迷倒。
使用说明:详细 Wiki

ios ViewController 的切换可以通过代码实现,也可以通过在 IB 中给ViewController 链接 Segue 来实现。两种方式各有优劣,也是根据个人习惯和实际需要来选择。TransitionTreasury 是通过代码来管理 ViewController 的跳转,使用起来比较简单。

作者提供的例子:

/// 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 来控制跳转,被认为是推荐的方式。比较好的库有 IBAnimatable ,该库的作者推崇 IB 的使用,对绘制界面能少用代码尽量不用。

CryptoSwift

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

常见的加解密过程都有,如果使用 Swift 作为开发语言,这是一个不可错过的库。

SwiftMessages

github 地址: SwiftMessages
简介:一个 Swift 编写的 Toast 控件。支持多种样式,当然也可以自定view。很强大。

to be continue...

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 199,519评论 5 468
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 83,842评论 2 376
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 146,544评论 0 330
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 53,742评论 1 271
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 62,646评论 5 359
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,027评论 1 275
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,513评论 3 390
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,169评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,324评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,268评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,299评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,996评论 3 315
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,591评论 3 303
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,667评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,911评论 1 255
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,288评论 2 345
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 41,871评论 2 341

推荐阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 11,977评论 4 60
  • 01. 手指轻触手机屏幕,给闺蜜发出信息:“你在干嘛?”一个小时后,手机屏幕终于出现信息:“我在lol呢。卧槽,刚...
    顽抗的小黑女阅读 665评论 2 2
  • 朋友雪和男朋友分手了,我点头,丝毫不觉得惊讶。 结果,大晚上,她拉着我回忆过去,我只能暗暗为我的生物钟默哀,这明明...
    三日初醒阅读 730评论 2 4
  • Outlook邮件模板 打开模板会自动添加签名,保存为模板前切记删除签名。 可以把常用的邮件模板打开后固定在任务栏列表:
    林万程阅读 2,680评论 0 2