Set of Swift utilities for your iOS apps.
The Swift 3 compatibility is available from the version >= 1.4, the previous versions are only supported by Swift 2.x
- For iOS 8+ projects with CocoaPods:
pod 'SwiftHelpSet', '~> 1.0'The networking layer is built over two different objects: the APIModule conforming object and the Resource.
Conform to the APIModule protocol like below:
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
}
}Then, create your own resource
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
}
)
}
}Then call your APIModule as below
UserModule().load(User.All) { result in
// switch over the Completion enum
}That increase the testability of your code by helping you injecting fake APIModule instances.
Thanks to @ChrisEidhof for the Resource idea.
The help set allows you to create a more functional project by using the Bindable type.
Declare your public variable as
var currentUser = Bindable<User>(user)Then, from your class, you can listen changes to the variable currentUser by doing
objectReference.currentUser.bind = { user in
// Do something when the currentUser changes
}Use Permission and Purchase to avoid the use of delegates for purchasing and for asking permissions. Use closures instead.
Use NotificationCenter to get a more powerful and smart wrapper around NSNotificationCenter
Use Each for a nice and easy implementation of NSTimer
Each(2).seconds.perform {
// Do something
return false // true for stopping the timer, false for continuing
}
Each(1).minutes.perfor {
// Do Something
}Use the method get(at index: Int) -> Element? on the array to get the element at index.
Added also the method remove<T: Equatable>(object: T) -> Int? that removes an equatable object from the array and returns the position if it was found
New properties to simply get more info about the string: isEmail, isPhoneNumber, isBlank, lenght and even localized that returns the localized string for the key
Easily check if an Int value is in a range by using isInRange(range: Range<Int>) -> Bool method.
Mix two dictionaries together by calling the +=, + operators
No more .OrderedAscending or .OrderedSame to compare two NSDate instances. You can use the operators >, <, =
Use the SwiftyTableView, SwiftyScrollView and the SwiftyTextField to avoid the use of delegates as below
let tableView = SwiftyTableView().configureNumberOfSections {
return 1
}.numberOfRowsPerSection { section in
return 2
}.cellForIndexPath { indexPath, tableView in
return UITableViewCell()
}Create your own animation easily. The wrapper allows you to create simple rotation animations only, but you can easily extend it.
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)Create your own table view controller easily, just passing a valid set of data
let genericTable = SwiftyGenericTableViewController<MyTableViewCell, User>()
genericTable.cellForModel { cell, model in
// setup the cell
return cell
}.onSelection { indexPath, model in
// push or do something
}.dataSource = userArrayDefine your action on UIControl trigger without using the ugly syntax addTarget(...).
let button = UIButton(frame: .zero)
button.bind(.TouchUpInside) {
// Do something
}Define your UIBarButtonItem easily by providing a closure instead of a selector.
let item = UIBarButtonItem.itemWith(title: "BUTTON") {
// do something on tap
}Easily define the UIGestureRecognizer by providing a closure instead of a selector
let tapRecognizer = UITapGestureRecognizer.recognizerPerforms {
// Do something on tap
}.numberOfTapsRequired = 1Create easily your new UIAlertController as an alert by calling the method alertWith.
Add new actions all in once by calling add(_ actions: [UIAlertAction]) on your UIAlertController instance
Create new pre-defined UIAlertAction by using the static instance .Cancel
let alertController = UIAlertController.alertWith(message: "An error occured")
let cancelAction = UIAlertAction.Cancel()
let doneAction = UIAlertAction.Done()
alertController.addActions([
doneAction,
cancelAction
])
// present the controller