SwiftHelpSet 1.4

SwiftHelpSet 1.4

测试已测试
Lang语言 SwiftSwift
许可证 MIT
发布上次发布2016年9月
SPM支持 SPM

dalu93 维护。



SwiftHelpSet

BuddyBuild

一组用于您的 iOS 应用的 Swift 工具

安装

  • ,使用 CocoaPods
pod 'SwiftHelpSet', '~> 1.0'

网络

网络层建立在两个不同的对象之上:符合 APIModule 协议的对象和 Resource。像下面这样符合 APIModule 协议

final class UserModule: APIModule {
    let baseAPIURL: String = "http://myserver.ext/api/user"

    func load<Object>(resource: Resource<Object>, completion: Completion<Object, NSError> -> ()) -> NSURLSession {

        // do the request, calling an API middleware
        // return the request
    }
}

然后,创建您自己的资源

final class User {
    let id: String

    init(id: String) {
        self.id = id
    }
}

extension User {
    static var All: Resource<[User]> {
        return Resource(
            endpoint: Endpoint(
                path: "/",
                method: .GET,
                parameters: nil,
                headers: nil
            ),
            parseJSON: { json -> [User] in
                // parse the JSON
                // return the array of User
            }
        )
    }
}

然后,按照以下方式调用您的 APIModule

UserModule().load(User.All) { result in
    // switch over the Completion enum
}

这通过帮助您注入假的 APIModule 实例,增加了您代码的可测试性。

感谢 @ChrisEidhof 提供的 Resource 灵感。

绑定

帮助集允许您通过使用 Bindable 类型创建更功能性的项目。将您的公开变量声明为

var currentUser = Bindable<User>(user)

然后,在您的类中,您可以通过以下方式监听 currentUser 变量的变化:

objectReference.currentUser.bind = { user in
    // Do something when the currentUser changes
}

Foundation 包装器

使用 PermissionPurchase 来避免对于购买和请求权限时使用 delegate,而是使用闭包。

使用 NotificationCenter 来获得对 NSNotificationCenter 的更强大和智能的包装

使用 Each 来实现一个优雅且简单的 NSTimer 实现

Each(2).seconds.perform {
    // Do something
    return false    // true for stopping the timer, false for continuing
}

Each(1).minutes.perfor {
    // Do Something
}

Foundation 扩展

数组

在数组上使用 get(at index: Int) -> Element? 方法来获取索引处的元素。还增加了方法 remove<T: Equatable>(object: T) -> Int?,它可以从数组中删除可比较的对象并返回其位置(如果找到的话)

字符串

新属性可以简单地获取更多有关字符串的信息:isEmailisPhoneNumberisBlanklength 以及甚至 localized,后者返回密钥的本地化字符串

Int

通过使用 isInRange(range: Range<Int>) -> Bool 方法,可以轻松检查一个 Int 值是否在某个范围内。

字典

通过调用 +=+ 运算符将两个字典混合在一起

NSDate

不再需要使用 .OrderedAscending.OrderedSame 来比较两个 NSDate 实例。您可以使用运算符 ><=

UIKit 包装器

使用 SwiftyTableViewSwiftyScrollViewSwiftyTextField 以避免使用以下方式使用代理

let tableView = SwiftyTableView().configureNumberOfSections {
    return 1
}.numberOfRowsPerSection { section in
    return 2
}.cellForIndexPath { indexPath, tableView in
    return UITableViewCell()
}

CABasicAnimation 包装器

轻松创建自己的动画。包装器允许您仅创建简单旋转动画,但您可以很容易地扩展它。

let animation = BasicAnimation.rotationAnimation(on: .z, duration: 1).onStart {
    // do something at the start of the animation
}.onStop { finished in
    // do something at the end
    animation.remove()
}.add(to: layer)

通用表格视图控制器

只需通过传递有效的一组数据,即可轻松创建自己的表格视图控制器

let genericTable = SwiftyGenericTableViewController<MyTableViewCell, User>()

genericTable.cellForModel { cell, model in
    // setup the cell
    return cell
}.onSelection { indexPath, model in
    // push or do something
}.dataSource = userArray

UIControl 扩展

无需使用难看的语法 addTarget(...) 即可定义 UIControl 触发动作。

let button = UIButton(frame: .zero)
button.bind(.TouchUpInside) {
    // Do something
}

UIBarButtonItem 扩展

通过提供一个闭包而不是选择器来轻松定义 UIBarButtonItem

let item = UIBarButtonItem.itemWith(title: "BUTTON") {
    // do something on tap
}

UIGestureRecognizer 扩展

通过提供一个闭包而不是选择器来轻松定义 UIGestureRecognizer

let tapRecognizer = UITapGestureRecognizer.recognizerPerforms {
    // Do something on tap
}.numberOfTapsRequired = 1

UIAlertController 和 UIAlertAction 扩展

通过调用方法 alertWith 轻松创建新的作为警报的 UIAlertController

通过在您的 UIAlertController 实例上调用 add(_ actions: [UIAlertAction]) 一次性添加新的操作。

使用静态实例 .Cancel 创建新的预定义 UIAlertAction

let alertController = UIAlertController.alertWith(message: "An error occured")

let cancelAction = UIAlertAction.Cancel()
let doneAction = UIAlertAction.Done()

alertController.addActions([
    doneAction,
    cancelAction
])

// present the controller