Skip to content

Commit a25345d

Browse files
committed
updated lib to swift3
1 parent bdd6c2a commit a25345d

File tree

4 files changed

+59
-59
lines changed

4 files changed

+59
-59
lines changed

iOSFormUtils/Form.swift

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public protocol FormDelegate {
2424

2525
- Return: The first input.
2626
*/
27-
func getFirstInput(form: Form) -> FormInput
27+
func getFirstInput(_ form: Form) -> FormInput
2828

2929
/*
3030
Returns the following input of a form input
@@ -34,12 +34,12 @@ public protocol FormDelegate {
3434

3535
- Return: If the current input is the last one, nil. If not, the following input.
3636
*/
37-
func getNextInput(form: Form, currentInput: FormInput) -> FormInput?
37+
func getNextInput(_ form: Form, currentInput: FormInput) -> FormInput?
3838
}
3939

4040
// MARK: Class
4141
/// UIScrollView child class for forms handling
42-
public class Form: UIScrollView {
42+
open class Form: UIScrollView {
4343
// MARK: Class properties
4444
/// The original frame of the form
4545
var originalFrame: CGRect!
@@ -51,10 +51,10 @@ public class Form: UIScrollView {
5151
var keyboardViewHeight: CGFloat = 216
5252

5353
/// The stored delegate
54-
public var formDelegate: FormDelegate!
54+
open var formDelegate: FormDelegate!
5555

5656
/// The current input which has been focused
57-
private var currentInput: FormInput! {
57+
fileprivate var currentInput: FormInput! {
5858
didSet {
5959
handleInputsReturnKeys()
6060
}
@@ -71,7 +71,7 @@ public class Form: UIScrollView {
7171
commonInit()
7272
}
7373

74-
override public func addSubview(view: UIView) {
74+
override open func addSubview(_ view: UIView) {
7575
super.addSubview(view)
7676

7777
if let input: FormInput = view as? FormInput {
@@ -84,25 +84,25 @@ public class Form: UIScrollView {
8484
/**
8585
Custom initializer
8686
*/
87-
private func commonInit() {
88-
NSNotificationCenter.defaultCenter().addObserver(
87+
fileprivate func commonInit() {
88+
NotificationCenter.default.addObserver(
8989
self,
9090
selector: #selector(Form.keyboardShown(_:)),
91-
name: UIKeyboardDidShowNotification,
91+
name: NSNotification.Name.UIKeyboardDidShow,
9292
object: nil
9393
)
9494

95-
NSNotificationCenter.defaultCenter().addObserver(
95+
NotificationCenter.default.addObserver(
9696
self,
9797
selector: #selector(Form.textFieldReturnedFired(_:)),
98-
name: tfReturnedNotifName,
98+
name: NSNotification.Name(rawValue: tfReturnedNotifName),
9999
object: nil
100100
)
101101

102-
NSNotificationCenter.defaultCenter().addObserver(
102+
NotificationCenter.default.addObserver(
103103
self,
104104
selector: #selector(Form.textFieldBecameFirstResponder(_:)),
105-
name: tfBecameFirstResponderNotifName,
105+
name: NSNotification.Name(rawValue: tfBecameFirstResponderNotifName),
106106
object: nil
107107
)
108108
if let _ = formDelegate {
@@ -113,14 +113,14 @@ public class Form: UIScrollView {
113113
/**
114114
Handles return keys type for inputs
115115
*/
116-
private func handleInputsReturnKeys() {
116+
fileprivate func handleInputsReturnKeys() {
117117
let inputs = getOrderedInputs()
118118
for input in inputs {
119119
if let textField: UITextField = input as? UITextField {
120120
if textField == inputs.last as? UITextField {
121-
textField.returnKeyType = .Go
121+
textField.returnKeyType = .go
122122
} else {
123-
textField.returnKeyType = .Next
123+
textField.returnKeyType = .next
124124
}
125125
}
126126
}
@@ -130,7 +130,7 @@ public class Form: UIScrollView {
130130
Updates the scrollview frame when keyboard appears.
131131
Scrolls to make the current field visible.
132132
*/
133-
private func minimizeScrollingZone(input: FormInput) {
133+
fileprivate func minimizeScrollingZone(_ input: FormInput) {
134134
if (!viewScrolledForKeyboard) {
135135
viewScrolledForKeyboard = true
136136
originalFrame = self.frame
@@ -144,15 +144,15 @@ public class Form: UIScrollView {
144144
self.frame = newFrame
145145
}
146146

147-
UIView.animateWithDuration(0.2) {
147+
UIView.animate(withDuration: 0.2, animations: {
148148
self.contentOffset = CGPoint(x: 0, y: input.frame.origin.y - self.frame.height/2 + input.frame.height/2)
149-
}
149+
})
150150
}
151151

152152
/**
153153
Resets the scrolling zone to its original value.
154154
*/
155-
private func resetScrollingZone() {
155+
fileprivate func resetScrollingZone() {
156156
viewScrolledForKeyboard = false
157157
if let _ = originalFrame {
158158
self.frame = originalFrame
@@ -166,7 +166,7 @@ public class Form: UIScrollView {
166166

167167
- Parameter notification: the received notification.
168168
*/
169-
func textFieldReturnedFired(notification: NSNotification) {
169+
func textFieldReturnedFired(_ notification: Notification) {
170170
if let textfield = notification.object as? FormInput {
171171
if isLastInput(textfield) {
172172
textfield.stopEditing()
@@ -187,12 +187,12 @@ public class Form: UIScrollView {
187187

188188
- Parameter notification: the received notification
189189
*/
190-
func keyboardShown(notification: NSNotification) {
191-
let info = notification.userInfo!
192-
let value: AnyObject = info[UIKeyboardFrameEndUserInfoKey]!
190+
func keyboardShown(_ notification: Notification) {
191+
let info = (notification as NSNotification).userInfo!
192+
let value: AnyObject = info[UIKeyboardFrameEndUserInfoKey]! as AnyObject
193193

194-
let rawFrame = value.CGRectValue
195-
let keyboardFrame = self.convertRect(rawFrame, fromView: nil)
194+
let rawFrame = value.cgRectValue
195+
let keyboardFrame = self.convert(rawFrame!, from: nil)
196196

197197
keyboardViewHeight = keyboardFrame.height
198198
}
@@ -202,7 +202,7 @@ public class Form: UIScrollView {
202202

203203
- Parameter notification: the received notification
204204
*/
205-
func textFieldBecameFirstResponder(notification: NSNotification) {
205+
func textFieldBecameFirstResponder(_ notification: Notification) {
206206
if let textfield = notification.object as? FormInput {
207207
currentInput = textfield
208208
}
@@ -213,7 +213,7 @@ public class Form: UIScrollView {
213213

214214
- Parameter input: the input to compare
215215
*/
216-
private func isLastInput(input: FormInput) -> Bool {
216+
fileprivate func isLastInput(_ input: FormInput) -> Bool {
217217
if let _ = formDelegate {
218218
if let nextInput: FormInput = formDelegate.getNextInput(self, currentInput: currentInput) {
219219
return false
@@ -244,11 +244,11 @@ public class Form: UIScrollView {
244244

245245
// MARK: Extensions
246246
extension Form: FormInputDelegate {
247-
public func didEnterEditionMode(input: FormInput) {
248-
dispatch_async(dispatch_get_main_queue()) {
247+
public func didEnterEditionMode(_ input: FormInput) {
248+
DispatchQueue.main.async {
249249
self.minimizeScrollingZone(input)
250250
}
251251
}
252252

253-
public func didExitEditionMode(input: FormInput) {}
253+
public func didExitEditionMode(_ input: FormInput) {}
254254
}

iOSFormUtils/FormInput.swift

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,25 +15,25 @@ let tfReturnedNotifName = "textFieldReturned"
1515

1616
/// Delegate protocol for FormInput
1717
public protocol FormInputDelegate {
18-
func didEnterEditionMode(input: FormInput)
19-
func didExitEditionMode(input: FormInput)
18+
func didEnterEditionMode(_ input: FormInput)
19+
func didExitEditionMode(_ input: FormInput)
2020
}
2121

2222
/// Data source protocol for FormInput
2323
public protocol FormInputDataSource {
24-
func applyCustomInit(input: FormInput)
24+
func applyCustomInit(_ input: FormInput)
2525
}
2626

2727
// MARK: Class
2828
/// UITextfield child class for easy form inputs handling
29-
public class FormInput: UITextField {
29+
open class FormInput: UITextField {
3030
// MARK: Class variables
31-
public var inputDataSource: FormInputDataSource!
32-
public var formInputDelegate: FormInputDelegate!
33-
private var inputAccessory: UIView!
34-
private var validationHandler: ValidatedFormInput!
35-
public var validationDelegate: ValidatedFormInputDelegate!
36-
public var validationDataSource: ValidatedFormInputDataSource!
31+
open var inputDataSource: FormInputDataSource!
32+
open var formInputDelegate: FormInputDelegate!
33+
fileprivate var inputAccessory: UIView!
34+
fileprivate var validationHandler: ValidatedFormInput!
35+
open var validationDelegate: ValidatedFormInputDelegate!
36+
open var validationDataSource: ValidatedFormInputDataSource!
3737

3838
// MARK: Superclass overrides
3939
override init(frame: CGRect) {
@@ -51,7 +51,7 @@ public class FormInput: UITextField {
5151
/**
5252
Main initializer
5353
*/
54-
public func commonInit() {
54+
open func commonInit() {
5555
self.delegate = self
5656
self.validationHandler = self
5757

@@ -65,32 +65,32 @@ public class FormInput: UITextField {
6565
/**
6666
Stops the current edition.
6767
*/
68-
public func stopEditing() {
69-
NSNotificationCenter.defaultCenter().postNotificationName(tfResignedFirstResponderNotifName, object: self)
68+
open func stopEditing() {
69+
NotificationCenter.default.post(name: Notification.Name(rawValue: tfResignedFirstResponderNotifName), object: self)
7070
self.resignFirstResponder()
7171
}
7272
}
7373

7474
// MARK: Extensions
7575
extension FormInput: UITextFieldDelegate {
76-
public func textFieldDidBeginEditing(textField: UITextField) {
76+
public func textFieldDidBeginEditing(_ textField: UITextField) {
7777
if let _ = formInputDelegate {
7878
formInputDelegate.didEnterEditionMode(self)
7979
}
8080
if let _ = validationDelegate {
8181
validationDelegate.didExitErrorMode(self)
8282
}
83-
NSNotificationCenter.defaultCenter().postNotificationName(tfBecameFirstResponderNotifName, object: self)
83+
NotificationCenter.default.post(name: Notification.Name(rawValue: tfBecameFirstResponderNotifName), object: self)
8484
}
8585

86-
public func textFieldDidEndEditing(textField: UITextField) {
86+
public func textFieldDidEndEditing(_ textField: UITextField) {
8787
if let _ = formInputDelegate {
8888
formInputDelegate.didExitEditionMode(self)
8989
}
9090
}
9191

92-
public func textFieldShouldReturn(textField: UITextField) -> Bool {
93-
NSNotificationCenter.defaultCenter().postNotificationName(tfReturnedNotifName, object: self)
92+
public func textFieldShouldReturn(_ textField: UITextField) -> Bool {
93+
NotificationCenter.default.post(name: Notification.Name(rawValue: tfReturnedNotifName), object: self)
9494

9595
return true;
9696
}

iOSFormUtils/ValidatedForm.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import Foundation
1010

1111
// MARK: Class
1212
/// Form class able to validates its fields
13-
public class ValidatedForm: Form {
13+
open class ValidatedForm: Form {
1414

1515
// MARK: Public own methods
1616

@@ -19,7 +19,7 @@ public class ValidatedForm: Form {
1919

2020
- Return: validation result
2121
*/
22-
public func validate() -> Bool {
22+
open func validate() -> Bool {
2323
for input in getOrderedInputs() {
2424
if (!input.validateFormat()) {
2525
return false

iOSFormUtils/ValidatedFormInput.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,14 @@ public protocol ValidatedFormInputDelegate {
4141
- Parameter input: The input
4242
- Parameter errorType: The error description
4343
*/
44-
func didEnterErrorMode(input: ValidatedFormInput, errorType: String)
44+
func didEnterErrorMode(_ input: ValidatedFormInput, errorType: String)
4545

4646
/**
4747
To update the input without the error mode.
4848

4949
- Parameter input: The input
5050
*/
51-
func didExitErrorMode(input: ValidatedFormInput)
51+
func didExitErrorMode(_ input: ValidatedFormInput)
5252
}
5353

5454
/// Data Source protocol for validated form
@@ -58,7 +58,7 @@ public protocol ValidatedFormInputDataSource {
5858

5959
- Parameter input: The input
6060
*/
61-
func validationTypeForInput(input: ValidatedFormInput) -> ValidatedFormInputType
61+
func validationTypeForInput(_ input: ValidatedFormInput) -> ValidatedFormInputType
6262
}
6363

6464
// MARK: Extensions
@@ -72,23 +72,23 @@ extension FormInput: ValidatedFormInput {
7272
}
7373
case .Email :
7474
let emailValidator: NSPredicate = NSPredicate(format: "SELF MATCHES %@", "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}")
75-
if (emailValidator.evaluateWithObject(self.text)) {
75+
if (emailValidator.evaluate(with: self.text)) {
7676
return true;
7777
}
7878
case .ZipCode :
7979
let zipCodeValidator: NSPredicate = NSPredicate(format: "SELF MATCHES %@", "((0[1-9])|([1-8][0-9])|(9[0-8])|(2A)|(2B))[0-9]{3}")
80-
if (zipCodeValidator.evaluateWithObject(self.text)) {
80+
if (zipCodeValidator.evaluate(with: self.text)) {
8181
return true;
8282
}
8383
case .Date :
84-
let dateFormatter = NSDateFormatter()
84+
let dateFormatter = DateFormatter()
8585
dateFormatter.dateFormat = "dd/MM/YYYY"
86-
if let _ = dateFormatter.dateFromString(self.text!) {
86+
if let _ = dateFormatter.date(from: self.text!) {
8787
return true
8888
}
8989
case .Phone :
9090
let phoneValidator: NSPredicate = NSPredicate(format: "SELF MATCHES %@", "(0[1-9]([-. ]?[0-9]{2}){4})")
91-
if (phoneValidator.evaluateWithObject(self.text)) {
91+
if (phoneValidator.evaluate(with: self.text)) {
9292
return true;
9393
}
9494
case .NoValidation :

0 commit comments

Comments
 (0)