Skip to content

Commit

Permalink
Merge pull request #52 from goodeggs/swift-2.0
Browse files Browse the repository at this point in the history
Migrate to Swift 2.0
  • Loading branch information
jpotts18 committed Sep 11, 2015
2 parents ca65096 + 45da08a commit d801538
Show file tree
Hide file tree
Showing 13 changed files with 39 additions and 37 deletions.
4 changes: 2 additions & 2 deletions SwiftValidator.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = "SwiftValidator"
s.version = "2.1.1"
s.version = "3.0.0"
s.summary = "A UITextField Validation library for Swift"
s.homepage = "https://github.com/jpotts18/SwiftValidator"
s.screenshots = "https://raw.githubusercontent.com/jpotts18/SwiftValidator/master/swift-validator-v2.gif"
Expand All @@ -9,7 +9,7 @@ Pod::Spec.new do |s|
s.social_media_url = "http://twitter.com/jpotts18"
s.platform = :ios
s.ios.deployment_target = '8.0'
s.source = { :git => "https://github.com/jpotts18/SwiftValidator.git", :tag => "2.1.1" }
s.source = { :git => "https://github.com/jpotts18/SwiftValidator.git", :tag => "3.0.0" }
s.source_files = "Validator/*.swift"
s.frameworks = ['Foundation', 'UIKit']
s.requires_arc = true
Expand Down
34 changes: 18 additions & 16 deletions Validator.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
62DC8D6E1AAA42CE0095DFA7 /* PasswordRule.swift in Sources */ = {isa = PBXBuildFile; fileRef = 62DC8D6B1AAA42CE0095DFA7 /* PasswordRule.swift */; };
62DC8D711AAA43110095DFA7 /* ZipCodeRule.swift in Sources */ = {isa = PBXBuildFile; fileRef = 62DC8D701AAA43110095DFA7 /* ZipCodeRule.swift */; };
62E9E2AD1ACFB336000A939C /* RegexRule.swift in Sources */ = {isa = PBXBuildFile; fileRef = 62E9E2AC1ACFB336000A939C /* RegexRule.swift */; };
D7C1D97E1BA25940008CE686 /* PhoneNumberRule.swift in Sources */ = {isa = PBXBuildFile; fileRef = D7C1D97D1BA25940008CE686 /* PhoneNumberRule.swift */; settings = {ASSET_TAGS = (); }; };
DC5A35EC1AF99BA60013FE6B /* FloatRule.swift in Sources */ = {isa = PBXBuildFile; fileRef = DC5A35EB1AF99BA60013FE6B /* FloatRule.swift */; };
/* End PBXBuildFile section */

Expand Down Expand Up @@ -63,6 +64,7 @@
62DC8D6B1AAA42CE0095DFA7 /* PasswordRule.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PasswordRule.swift; sourceTree = "<group>"; };
62DC8D701AAA43110095DFA7 /* ZipCodeRule.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ZipCodeRule.swift; sourceTree = "<group>"; };
62E9E2AC1ACFB336000A939C /* RegexRule.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RegexRule.swift; sourceTree = "<group>"; };
D7C1D97D1BA25940008CE686 /* PhoneNumberRule.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PhoneNumberRule.swift; sourceTree = "<group>"; };
DC5A35EB1AF99BA60013FE6B /* FloatRule.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FloatRule.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

Expand Down Expand Up @@ -163,17 +165,18 @@
62DC8D661AAA42700095DFA7 /* Rules */ = {
isa = PBXGroup;
children = (
628637271AAA49E300BC8FCF /* ConfirmRule.swift */,
62DC8D691AAA42CE0095DFA7 /* EmailRule.swift */,
DC5A35EB1AF99BA60013FE6B /* FloatRule.swift */,
62DC8D671AAA42920095DFA7 /* FullNameRule.swift */,
623F29931AD836E6005B26CB /* MaxLengthRule.swift */,
628637251AAA474B00BC8FCF /* MinLengthRule.swift */,
62DC8D641AAA42520095DFA7 /* Rule.swift */,
62DC8D691AAA42CE0095DFA7 /* EmailRule.swift */,
62DC8D6A1AAA42CE0095DFA7 /* RequiredRule.swift */,
62DC8D6B1AAA42CE0095DFA7 /* PasswordRule.swift */,
62DC8D671AAA42920095DFA7 /* FullNameRule.swift */,
62DC8D701AAA43110095DFA7 /* ZipCodeRule.swift */,
628637271AAA49E300BC8FCF /* ConfirmRule.swift */,
D7C1D97D1BA25940008CE686 /* PhoneNumberRule.swift */,
62E9E2AC1ACFB336000A939C /* RegexRule.swift */,
DC5A35EB1AF99BA60013FE6B /* FloatRule.swift */,
62DC8D6A1AAA42CE0095DFA7 /* RequiredRule.swift */,
62DC8D641AAA42520095DFA7 /* Rule.swift */,
62DC8D701AAA43110095DFA7 /* ZipCodeRule.swift */,
);
name = Rules;
sourceTree = "<group>";
Expand Down Expand Up @@ -222,7 +225,8 @@
62D1AE0F1A1E6D4400E4DFF8 /* Project object */ = {
isa = PBXProject;
attributes = {
LastUpgradeCheck = 0610;
LastSwiftMigration = 0700;
LastUpgradeCheck = 0700;
ORGANIZATIONNAME = jpotts18;
TargetAttributes = {
62D1AE161A1E6D4400E4DFF8 = {
Expand Down Expand Up @@ -283,6 +287,7 @@
62D1AE4F1A1E6FF800E4DFF8 /* ValidationError.swift in Sources */,
62DC8D6E1AAA42CE0095DFA7 /* PasswordRule.swift in Sources */,
62DC8D6C1AAA42CE0095DFA7 /* EmailRule.swift in Sources */,
D7C1D97E1BA25940008CE686 /* PhoneNumberRule.swift in Sources */,
628637281AAA49E300BC8FCF /* ConfirmRule.swift in Sources */,
62DC8D651AAA42520095DFA7 /* Rule.swift in Sources */,
62D1AE1F1A1E6D4400E4DFF8 /* ViewController.swift in Sources */,
Expand Down Expand Up @@ -355,6 +360,7 @@
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
COPY_PHASE_STRIP = NO;
ENABLE_STRICT_OBJC_MSGSEND = YES;
ENABLE_TESTABILITY = YES;
GCC_C_LANGUAGE_STANDARD = gnu99;
GCC_DYNAMIC_NO_PIC = NO;
GCC_OPTIMIZATION_LEVEL = 0;
Expand Down Expand Up @@ -418,6 +424,7 @@
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
INFOPLIST_FILE = Validator/Info.plist;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = "me.jeffpotter.$(PRODUCT_NAME:rfc1034identifier)";
PRODUCT_NAME = "$(TARGET_NAME)";
};
name = Debug;
Expand All @@ -428,6 +435,7 @@
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
INFOPLIST_FILE = Validator/Info.plist;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = "me.jeffpotter.$(PRODUCT_NAME:rfc1034identifier)";
PRODUCT_NAME = "$(TARGET_NAME)";
};
name = Release;
Expand All @@ -436,16 +444,13 @@
isa = XCBuildConfiguration;
buildSettings = {
BUNDLE_LOADER = "$(TEST_HOST)";
FRAMEWORK_SEARCH_PATHS = (
"$(SDKROOT)/Developer/Library/Frameworks",
"$(inherited)",
);
GCC_PREPROCESSOR_DEFINITIONS = (
"DEBUG=1",
"$(inherited)",
);
INFOPLIST_FILE = ValidatorTests/Info.plist;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = "me.jeffpotter.$(PRODUCT_NAME:rfc1034identifier)";
PRODUCT_NAME = "$(TARGET_NAME)";
TEST_HOST = "$(BUILT_PRODUCTS_DIR)/Validator.app/Validator";
};
Expand All @@ -455,12 +460,9 @@
isa = XCBuildConfiguration;
buildSettings = {
BUNDLE_LOADER = "$(TEST_HOST)";
FRAMEWORK_SEARCH_PATHS = (
"$(SDKROOT)/Developer/Library/Frameworks",
"$(inherited)",
);
INFOPLIST_FILE = ValidatorTests/Info.plist;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = "me.jeffpotter.$(PRODUCT_NAME:rfc1034identifier)";
PRODUCT_NAME = "$(TARGET_NAME)";
TEST_HOST = "$(BUILT_PRODUCTS_DIR)/Validator.app/Validator";
};
Expand Down
4 changes: 2 additions & 2 deletions Validator/FloatRule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ public class FloatRule:Rule {
}

public func validate(value: String) -> Bool {
let regex = NSRegularExpression(pattern: "[-+]?(\\d*[.])?\\d+", options: nil, error: nil)
let regex = try? NSRegularExpression(pattern: "[-+]?(\\d*[.])?\\d+", options: [])
if let regex = regex {
let match = regex.numberOfMatchesInString(value, options: nil, range: NSRange(location: 0, length: count(value)))
let match = regex.numberOfMatchesInString(value, options: [], range: NSRange(location: 0, length: value.characters.count))
return match == 1
}
return false
Expand Down
2 changes: 1 addition & 1 deletion Validator/FullNameRule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class FullNameRule : Rule {
}

public func validate(value: String) -> Bool {
var nameArray: [String] = split(value) { $0 == " " }
let nameArray: [String] = value.characters.split { $0 == " " }.map { String($0) }
return nameArray.count >= 2
}

Expand Down
2 changes: 1 addition & 1 deletion Validator/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<key>CFBundleExecutable</key>
<string>$(EXECUTABLE_NAME)</string>
<key>CFBundleIdentifier</key>
<string>me.jeffpotter.$(PRODUCT_NAME:rfc1034identifier)</string>
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
Expand Down
2 changes: 1 addition & 1 deletion Validator/MaxLengthRule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class MaxLengthRule: Rule {
}

public func validate(value: String) -> Bool {
return count(value) <= DEFAULT_LENGTH
return value.characters.count <= DEFAULT_LENGTH
}

public func errorMessage() -> String {
Expand Down
2 changes: 1 addition & 1 deletion Validator/MinLengthRule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class MinLengthRule: Rule {
}

public func validate(value: String) -> Bool {
return count(value) >= DEFAULT_LENGTH
return value.characters.count >= DEFAULT_LENGTH
}

public func errorMessage() -> String {
Expand Down
2 changes: 1 addition & 1 deletion Validator/PhoneNumberRule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class PhoneNumberRule: Rule {
}

func validate(value: String) -> Bool {
var phoneTest = NSPredicate(format: "SELF MATCHES %@", PHONE_REGEX)
let phoneTest = NSPredicate(format: "SELF MATCHES %@", PHONE_REGEX)
return phoneTest.evaluateWithObject(value)
}

Expand Down
2 changes: 1 addition & 1 deletion Validator/ValidationError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import Foundation
import UIKit

public class ValidationError {
public class ValidationError: NSObject {
public let textField:UITextField
public var errorLabel:UILabel?
public let errorMessage:String
Expand Down
2 changes: 1 addition & 1 deletion Validator/ValidationRule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class ValidationRule {
}

public func validateField() -> ValidationError? {
return rules.filter{ !$0.validate(self.textField.text) }
return rules.filter{ !$0.validate(self.textField.text ?? "") }
.map{ rule -> ValidationError in return ValidationError(textField: self.textField, errorLabel:self.errorLabel, error: rule.errorMessage()) }.first
}
}
4 changes: 2 additions & 2 deletions Validator/Validator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public class Validator {
errors = [:]

for (textField, rule) in validations {
if var error = rule.validateField() {
if let error = rule.validateField() {
errors[textField] = error

// let the user transform the field if they want
Expand All @@ -49,7 +49,7 @@ public class Validator {

// MARK: Using Keys

public func styleTransformers(#success:((validationRule:ValidationRule)->Void)?, error:((validationError:ValidationError)->Void)?) {
public func styleTransformers(success success:((validationRule:ValidationRule)->Void)?, error:((validationError:ValidationError)->Void)?) {
self.successStyleTransform = success
self.errorStyleTransform = error
}
Expand Down
14 changes: 7 additions & 7 deletions Validator/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,15 @@ class ViewController: UIViewController , ValidationDelegate, UITextFieldDelegate
self.view.addGestureRecognizer(UITapGestureRecognizer(target: self, action: "hideKeyboard"))

validator.styleTransformers(success:{ (validationRule) -> Void in
println("here")
print("here")
// clear error label
validationRule.errorLabel?.hidden = true
validationRule.errorLabel?.text = ""
validationRule.textField.layer.borderColor = UIColor.greenColor().CGColor
validationRule.textField.layer.borderWidth = 0.5

}, error:{ (validationError) -> Void in
println("error")
print("error")
validationError.errorLabel?.hidden = false
validationError.errorLabel?.text = validationError.errorMessage
validationError.textField.layer.borderColor = UIColor.redColor().CGColor
Expand All @@ -57,22 +57,22 @@ class ViewController: UIViewController , ValidationDelegate, UITextFieldDelegate
}

@IBAction func submitTapped(sender: AnyObject) {
println("Validating...")
print("Validating...")
validator.validate(self)
}

// MARK: ValidationDelegate Methods

func validationSuccessful() {
println("Validation Success!")
var alert = UIAlertController(title: "Success", message: "You are validated!", preferredStyle: UIAlertControllerStyle.Alert)
var defaultAction = UIAlertAction(title: "OK", style: .Default, handler: nil)
print("Validation Success!")
let alert = UIAlertController(title: "Success", message: "You are validated!", preferredStyle: UIAlertControllerStyle.Alert)
let defaultAction = UIAlertAction(title: "OK", style: .Default, handler: nil)
alert.addAction(defaultAction)
self.presentViewController(alert, animated: true, completion: nil)

}
func validationFailed(errors:[UITextField:ValidationError]) {
println("Validation FAILED!")
print("Validation FAILED!")
}

func hideKeyboard(){
Expand Down
2 changes: 1 addition & 1 deletion ValidatorTests/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<key>CFBundleExecutable</key>
<string>$(EXECUTABLE_NAME)</string>
<key>CFBundleIdentifier</key>
<string>me.jeffpotter.$(PRODUCT_NAME:rfc1034identifier)</string>
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
Expand Down

0 comments on commit d801538

Please sign in to comment.