Set of Swift utilities for your iOS apps
- 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
}
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 the method get(at index: Int) -> Element?
on the array to secure 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 get simply 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 +=
operator
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()
tableView.configureNumberOfSections = { return 1 }
tableView.numberOfRowsPerSection = { section in
return 2
}
tableView.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)
animation.onStart = { animation in
// do something at the start of the animation
}
animation.onStop = { animation, finished in
// do something at the end
animation.remove()
}
animation.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
}
genericTable.onSelection = { indexPath, model in
// push or do something
}
genericTable.dataSource = userArray
Define your action on UIControl
trigger without using the ugly syntax addTarget(...)
.
let button = UIButton(frame: .zero).bind(.TouchUpInside) { button in
// Do something
}