Skip to content

Commit 43fddfb

Browse files
Merge pull request #161 from jpotts18/swift-3
Swift 3
2 parents 3dc5b79 + 1efab45 commit 43fddfb

26 files changed

Lines changed: 123 additions & 101 deletions

.travis.yml

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
language: objective-c
2-
osx_image: xcode7.1
2+
osx_image: xcode8
3+
xcode_sdk: iphonesimulator10.0
4+
xcode_project: Validator.xcodeproj
5+
xcode_scheme: Validator
36

47
before_install:
5-
8+
- gem install cocoapods -v '0.32.1'
69
- gem install xcpretty --no-rdoc --no-ri --no-document --quiet
710

811
script:
9-
12+
- xcodebuild clean build test -project Validator.xcodeproj -scheme Validator -sdk iphonesimulator -destination "platform=iOS Simulator,OS=10.0,name=iPhone 6" -enableCodeCoverage YES CODE_SIGNING_REQUIRED=NO | xcpretty
1013
- pod lib lint
11-
- xcodebuild clean build test -project Validator.xcodeproj -scheme Validator -sdk iphonesimulator9.1 -destination "OS=9.1,name=iPhone 6" -enableCodeCoverage YES | xcpretty
12-
1314
after_success:
1415

1516
- bash <(curl -s https://codecov.io/bash)

SwiftValidator/Core/Validatable.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
import Foundation
1010

11-
public typealias ValidatableField = protocol<AnyObject, Validatable>
11+
public typealias ValidatableField = AnyObject & Validatable
1212

1313
public protocol Validatable {
1414

SwiftValidator/Core/ValidationDelegate.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,5 @@ public protocol ValidationDelegate {
2323

2424
- returns: No return value.
2525
*/
26-
func validationFailed(errors: [(Validatable, ValidationError)])
26+
func validationFailed(_ errors: [(Validatable, ValidationError)])
2727
}

SwiftValidator/Core/Validator.swift

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ public class Validator {
2020
/// Dictionary to hold fields by their object identifiers
2121
private var fields = ValidatorDictionary<Validatable>()
2222
/// Variable that holds success closure to display positive status of field.
23-
private var successStyleTransform:((validationRule:ValidationRule)->Void)?
23+
private var successStyleTransform:((_ validationRule:ValidationRule)->Void)?
2424
/// Variable that holds error closure to display negative status of field.
25-
private var errorStyleTransform:((validationError:ValidationError)->Void)?
25+
private var errorStyleTransform:((_ validationError:ValidationError)->Void)?
2626
/// - returns: An initialized object, or nil if an object could not be created for some reason that would not result in an exception.
2727
public init(){}
2828

@@ -44,13 +44,13 @@ public class Validator {
4444

4545
// let the user transform the field if they want
4646
if let transform = self.errorStyleTransform {
47-
transform(validationError: error)
47+
transform(error)
4848
}
4949
} else {
5050
// No error
5151
// let the user transform the field if they want
5252
if let transform = self.successStyleTransform {
53-
transform(validationRule: rule)
53+
transform(rule)
5454
}
5555
}
5656
}
@@ -65,22 +65,22 @@ public class Validator {
6565
- parameter field: Holds validator field data.
6666
- returns: No return value.
6767
*/
68-
public func validateField(field: ValidatableField, callback: (error:ValidationError?) -> Void){
68+
public func validateField(_ field: ValidatableField, callback: (_ error:ValidationError?) -> Void){
6969
if let fieldRule = validations[field] {
7070
if let error = fieldRule.validateField() {
7171
errors[field] = error
7272
if let transform = self.errorStyleTransform {
73-
transform(validationError: error)
73+
transform(error)
7474
}
75-
callback(error: error)
75+
callback(error)
7676
} else {
7777
if let transform = self.successStyleTransform {
78-
transform(validationRule: fieldRule)
78+
transform(fieldRule)
7979
}
80-
callback(error: nil)
80+
callback(nil)
8181
}
8282
} else {
83-
callback(error: nil)
83+
callback(nil)
8484
}
8585
}
8686

@@ -93,7 +93,7 @@ public class Validator {
9393
- parameter error: A closure which is called with validationError, an object that holds validation error data
9494
- returns: No return value
9595
*/
96-
public func styleTransformers(success success:((validationRule:ValidationRule)->Void)?, error:((validationError:ValidationError)->Void)?) {
96+
public func styleTransformers(success:((_ validationRule:ValidationRule)->Void)?, error:((_ validationError:ValidationError)->Void)?) {
9797
self.successStyleTransform = success
9898
self.errorStyleTransform = error
9999
}
@@ -106,7 +106,7 @@ public class Validator {
106106
- parameter rules: A Rule array that holds different rules that apply to said field.
107107
- returns: No return value
108108
*/
109-
public func registerField(field: ValidatableField, errorLabel:UILabel? = nil, rules:[Rule]) {
109+
public func registerField(_ field: ValidatableField, errorLabel:UILabel? = nil, rules:[Rule]) {
110110
validations[field] = ValidationRule(field: field, rules:rules, errorLabel:errorLabel)
111111
fields[field] = field
112112
}
@@ -117,7 +117,7 @@ public class Validator {
117117
- parameter field: field used to locate and remove field from validator.
118118
- returns: No return value
119119
*/
120-
public func unregisterField(field:ValidatableField) {
120+
public func unregisterField(_ field:ValidatableField) {
121121
validations.removeValueForKey(field)
122122
errors.removeValueForKey(field)
123123
}
@@ -127,7 +127,7 @@ public class Validator {
127127

128128
- returns: No return value.
129129
*/
130-
public func validate(delegate:ValidationDelegate) {
130+
public func validate(_ delegate:ValidationDelegate) {
131131

132132
self.validateAllFields()
133133

@@ -145,10 +145,10 @@ public class Validator {
145145
- parameter callback: A closure which is called with errors, a dictionary of type Validatable:ValidationError.
146146
- returns: No return value.
147147
*/
148-
public func validate(callback:(errors:[(Validatable, ValidationError)])->Void) -> Void {
148+
public func validate(_ callback:(_ errors:[(Validatable, ValidationError)])->Void) -> Void {
149149

150150
self.validateAllFields()
151151

152-
callback(errors: errors.map { (fields[$1.field]!, $1) } )
152+
callback(errors.map { (fields[$1.field]!, $1) } )
153153
}
154154
}

SwiftValidator/Core/ValidatorDictionary.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
import Foundation
1010

11-
public struct ValidatorDictionary<T> : SequenceType {
11+
public struct ValidatorDictionary<T> : Sequence {
1212

1313
private var innerDictionary: [ObjectIdentifier: T] = [:];
1414

@@ -31,15 +31,15 @@ public struct ValidatorDictionary<T> : SequenceType {
3131
innerDictionary.removeAll()
3232
}
3333

34-
public mutating func removeValueForKey(key: ValidatableField) {
35-
innerDictionary.removeValueForKey(ObjectIdentifier(key))
34+
public mutating func removeValueForKey(_ key: ValidatableField) {
35+
innerDictionary.removeValue(forKey: ObjectIdentifier(key))
3636
}
3737

3838
public var isEmpty: Bool {
3939
return innerDictionary.isEmpty
4040
}
4141

42-
public func generate() -> DictionaryGenerator<ObjectIdentifier ,T> {
43-
return innerDictionary.generate()
42+
public func makeIterator() -> DictionaryIterator<ObjectIdentifier ,T> {
43+
return innerDictionary.makeIterator()
4444
}
4545
}

SwiftValidator/Rules/AlphaNumericRule.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,6 @@ public class AlphaNumericRule: CharacterSetRule {
2121
- returns: An initialized object, or nil if an object could not be created for some reason that would not result in an exception.
2222
*/
2323
public init(message: String = "Enter valid numeric characters") {
24-
super.init(characterSet: NSCharacterSet.alphanumericCharacterSet(), message: message)
24+
super.init(characterSet: CharacterSet.alphanumerics, message: message)
2525
}
26-
}
26+
}

SwiftValidator/Rules/AlphaRule.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,6 @@ public class AlphaRule: CharacterSetRule {
2121
- returns: An initialized object, or nil if an object could not be created for some reason.
2222
*/
2323
public init(message: String = "Enter valid alphabetic characters") {
24-
super.init(characterSet: NSCharacterSet.letterCharacterSet(), message: message)
24+
super.init(characterSet: CharacterSet.letters, message: message)
2525
}
26-
}
26+
}

SwiftValidator/Rules/CharacterSetRule.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import Foundation
1313
*/
1414
public class CharacterSetRule: Rule {
1515
/// NSCharacter that hold set of valid characters to hold
16-
private let characterSet: NSCharacterSet
16+
private let characterSet: CharacterSet
1717
/// String that holds error message
1818
private var message: String
1919

@@ -24,7 +24,7 @@ public class CharacterSetRule: Rule {
2424
- parameter message: String of error message.
2525
- returns: An initialized object, or nil if an object could not be created for some reason that would not result in an exception.
2626
*/
27-
public init(characterSet: NSCharacterSet, message: String = "Enter valid alpha") {
27+
public init(characterSet: CharacterSet, message: String = "Enter valid alpha") {
2828
self.characterSet = characterSet
2929
self.message = message
3030
}
@@ -35,9 +35,9 @@ public class CharacterSetRule: Rule {
3535
- parameter value: String to checked for validation.
3636
- returns: Boolean value. True if validation is successful; False if validation fails.
3737
*/
38-
public func validate(value: String) -> Bool {
38+
public func validate(_ value: String) -> Bool {
3939
for uni in value.unicodeScalars {
40-
if !characterSet.longCharacterIsMember(uni.value) {
40+
guard let uniVal = UnicodeScalar(uni.value), characterSet.contains(uniVal) else {
4141
return false
4242
}
4343
}
@@ -52,4 +52,4 @@ public class CharacterSetRule: Rule {
5252
public func errorMessage() -> String {
5353
return message
5454
}
55-
}
55+
}

SwiftValidator/Rules/ConfirmRule.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public class ConfirmationRule: Rule {
3737
- parameter value: String to checked for validation.
3838
- returns: A boolean value. True if validation is successful; False if validation fails.
3939
*/
40-
public func validate(value: String) -> Bool {
40+
public func validate(_ value: String) -> Bool {
4141
return confirmField.validationText == value
4242
}
4343

@@ -49,4 +49,4 @@ public class ConfirmationRule: Rule {
4949
public func errorMessage() -> String {
5050
return message
5151
}
52-
}
52+
}

SwiftValidator/Rules/ExactLengthRule.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public class ExactLengthRule : Rule {
2626
*/
2727
public init(length: Int, message : String = "Must be exactly %ld characters long"){
2828
self.length = length
29-
self.message = NSString(format: message, self.length) as String
29+
self.message = String(format: message, self.length)
3030
}
3131

3232
/**
@@ -35,7 +35,7 @@ public class ExactLengthRule : Rule {
3535
- parameter value: String to checked for validation.
3636
- returns: A boolean value. True if validation is successful; False if validation fails.
3737
*/
38-
public func validate(value: String) -> Bool {
38+
public func validate(_ value: String) -> Bool {
3939
return value.characters.count == length
4040
}
4141

@@ -47,4 +47,4 @@ public class ExactLengthRule : Rule {
4747
public func errorMessage() -> String {
4848
return message
4949
}
50-
}
50+
}

0 commit comments

Comments
 (0)