Skip to content

Commit ff25465

Browse files
committed
Updating for swift 1.2
1 parent ae514e2 commit ff25465

File tree

7 files changed

+99
-107
lines changed

7 files changed

+99
-107
lines changed

Validator/Base.lproj/Main.storyboard

Lines changed: 59 additions & 59 deletions
Large diffs are not rendered by default.

Validator/EmailRule.swift

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,31 +10,25 @@ import Foundation
1010

1111
class EmailRule: Rule {
1212

13-
let REGEX = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,6}"
13+
var REGEX : String
1414

15-
init(){}
16-
17-
init(regex:String){
18-
self.REGEX = regex
15+
init(){
16+
self.REGEX = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,6}"
1917
}
2018

21-
var message:String {
22-
return "Must be a valid email address"
19+
init(regex:String){
20+
REGEX = regex
2321
}
2422

25-
func validate(value:String) -> Bool {
26-
27-
if let emailTest = NSPredicate(format: "SELF MATCHES %@", REGEX) {
28-
if emailTest.evaluateWithObject(value) {
29-
return true
30-
} else {
31-
return false
32-
}
23+
func validate(value: String) -> Bool {
24+
let test = NSPredicate(format: "SELF MATCHES %@", self.REGEX)
25+
if test.evaluateWithObject(value) {
26+
return true
3327
}
3428
return false
3529
}
3630

3731
func errorMessage() -> String {
38-
return self.message
32+
return "Must be a valid email address"
3933
}
4034
}

Validator/MinLengthRule.swift

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,24 @@ import Foundation
1111

1212
class MinLengthRule : Rule {
1313

14-
let DEFAULT_MIN_LENGTH:Int = 3
14+
private let DEFAULT_MIN_LENGTH: Int
1515

16-
init(){}
16+
init(){
17+
DEFAULT_MIN_LENGTH = 3
18+
}
1719

1820
init(length:Int){
1921
self.DEFAULT_MIN_LENGTH = length
2022
}
2123

22-
func errorMessage() -> String {
23-
return "Must be at least \(DEFAULT_MIN_LENGTH) characters long"
24-
}
25-
2624
func validate(value: String) -> Bool {
27-
if countElements(value) <= DEFAULT_MIN_LENGTH {
25+
if count(value) <= DEFAULT_MIN_LENGTH {
2826
return false
2927
}
3028
return true
3129
}
30+
31+
func errorMessage() -> String {
32+
return "Must be at least \(DEFAULT_MIN_LENGTH) characters long"
33+
}
3234
}

Validator/PasswordRule.swift

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,29 +20,25 @@ class PasswordRule : Rule {
2020

2121
// 8 characters. one uppercase
2222

23-
var REGEX = "^(?=.*?[A-Z]).{8,}$"
23+
private let REGEX: String
2424

25-
init(){}
25+
init(){
26+
self.REGEX = "^(?=.*?[A-Z]).{8,}$"
27+
}
2628

2729
init(regex:String){
2830
self.REGEX = regex
2931
}
3032

31-
var message:String {
32-
return "Must be 8 characters with 1 uppercase"
33-
}
34-
3533
func validate(value: String) -> Bool {
36-
if let passwordTes = NSPredicate(format: "SELF MATCHES %@", REGEX) {
37-
if passwordTes.evaluateWithObject(value) {
38-
return true
39-
}
40-
return false
34+
let test = NSPredicate(format: "SELF MATCHES %@", self.REGEX)
35+
if test.evaluateWithObject(value) {
36+
return true
4137
}
4238
return false
4339
}
4440

4541
func errorMessage() -> String {
46-
return self.message
42+
return "Must be 8 characters with 1 uppercase"
4743
}
4844
}

Validator/ViewController.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ class ViewController: UIViewController , ValidationDelegate, UITextFieldDelegate
3131
override func viewDidLoad() {
3232
super.viewDidLoad()
3333

34+
self.view.addGestureRecognizer(UITapGestureRecognizer(target: self, action: "hideKeyboard"))
3435

3536
validator.registerField(fullNameTextField, errorLabel: fullNameErrorLabel , rules: [RequiredRule(), FullNameRule()])
3637
validator.registerField(emailTextField, errorLabel: emailErrorLabel, rules: [RequiredRule(), EmailRule()])
@@ -96,6 +97,10 @@ class ViewController: UIViewController , ValidationDelegate, UITextFieldDelegate
9697
error.errorLabel?.hidden = true
9798
}
9899
}
100+
101+
func hideKeyboard(){
102+
self.view.endEditing(true)
103+
}
99104

100105
}
101106

Validator/ZipCodeRule.swift

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,30 +9,25 @@
99
import Foundation
1010

1111
class ZipCodeRule: Rule {
12-
let REGEX = "\\d{5}"
12+
private let REGEX: String
1313

14-
15-
init(){}
14+
init(){
15+
self.REGEX = "\\d{5}"
16+
}
1617
init(regex:String){
1718
self.REGEX = regex
1819
}
1920

20-
var message: String {
21-
return "Enter a valid 5 digit zipcode"
22-
}
23-
2421
func validate(value: String) -> Bool {
25-
if let zipTest = NSPredicate(format: "SELF MATCHES %@", REGEX) {
26-
if zipTest.evaluateWithObject(value) {
27-
return true
28-
}
29-
return false
22+
let test = NSPredicate(format: "SELF MATCHES %@", self.REGEX)
23+
if test.evaluateWithObject(value) {
24+
return true
3025
}
3126
return false
3227
}
3328

3429
func errorMessage() -> String {
35-
return message
30+
return "Enter a valid 5 digit zipcode"
3631
}
3732

3833
}

0 commit comments

Comments
 (0)