Last active
March 17, 2017 14:23
-
-
Save DonMag/935ca4aa14c6e06d090f60dbc400ac20 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //: Playground - noun: a place where people can play | |
| import UIKit | |
| // peopleArray will be an Array of Dictionary objects | |
| let thePeopleArray = [ | |
| [ "firstName": "mom mobile", "phone": "242-1111"], | |
| [ "firstName": "mom work", "phone": "242-2222"], | |
| [ "firstName": "mom", "lastName": "car", "phone": "242-3333"], | |
| [ "firstName": "mom", "lastName": "home", "phone": "242-3333"], | |
| [ "firstName": "Xom", "lastName": "mobile", "phone": "242-4444"], | |
| [ "firstName": "Tom", "lastName": "Smith", "email": "test@mac.com"], | |
| [ "firstName": "John", "phone": "111-1111"], | |
| [ "firstName": "John mobile", "phone": "222-2222"], | |
| [ "firstName": "Johny", "lastName": "mobile", "phone": "333-3333"], | |
| [ "firstName": "Johny2", "lastName": "mobile", "phone": "444-4444"], | |
| [ "firstName": "Sam", "phone": "777-7777"], | |
| [ "lastName": "Sam", "phone": "888-8888"], | |
| ] | |
| func findPeopleMatching(word1: String, word2: String, inArray: [[String: String]]) -> [[String: String]]? { | |
| var resultArray = [[String: String]]() | |
| var attributeValue = "" | |
| var namePredicate: NSPredicate? | |
| // First you look for an exact match: FirstName == FirstWord AND LastName == SecondWord | |
| resultArray = inArray.filter { $0["firstName"]?.lowercaseString == word1.lowercaseString && $0["lastName"]?.lowercaseString == word2.lowercaseString } | |
| if resultArray.count > 0 { | |
| return resultArray | |
| } | |
| // If no match, look for a FirstName == "FirstWord SecondWord" | |
| resultArray = inArray.filter { $0["firstName"]?.lowercaseString == word1.lowercaseString + " " + word2.lowercaseString } | |
| if resultArray.count > 0 { | |
| return resultArray | |
| } | |
| // If no match, look for FirstName == FirstWord OR LastName == SecondWord | |
| resultArray = inArray.filter { $0["firstName"]?.lowercaseString == word1.lowercaseString || $0["lastName"]?.lowercaseString == word2.lowercaseString } | |
| if resultArray.count > 0 { | |
| return resultArray | |
| } | |
| // If no match, look for FirstName Begins-With Word1 | |
| attributeValue = word1 + "*"; | |
| namePredicate = NSPredicate(format: "firstName like[c] %@", attributeValue) | |
| resultArray = inArray.filter { namePredicate!.evaluateWithObject($0) }; | |
| if resultArray.count > 0 { | |
| return resultArray | |
| } | |
| // If no match, look for LastName Begins-With Word2 | |
| attributeValue = word2 + "*"; | |
| namePredicate = NSPredicate(format: "lastName like[c] %@", attributeValue) | |
| resultArray = inArray.filter { namePredicate!.evaluateWithObject($0) }; | |
| if resultArray.count > 0 { | |
| return resultArray | |
| } | |
| // If no match, look for Word1 is Part-Of FirstName | |
| attributeValue = "*" + word1 + "*"; | |
| namePredicate = NSPredicate(format: "firstName like[c] %@", attributeValue) | |
| resultArray = inArray.filter { namePredicate!.evaluateWithObject($0) }; | |
| if resultArray.count > 0 { | |
| return resultArray | |
| } | |
| // If no match, look for Word2 is Part-Of LastName | |
| attributeValue = "*" + word2 + "*"; | |
| namePredicate = NSPredicate(format: "lastName like[c] %@", attributeValue) | |
| resultArray = inArray.filter { namePredicate!.evaluateWithObject($0) }; | |
| if resultArray.count > 0 { | |
| return resultArray | |
| } | |
| // NO matches found, so return nil | |
| return nil | |
| } | |
| var word1 = "mom" | |
| var word2 = "mobile" | |
| if let rv = findPeopleMatching(word1, word2: word2, inArray: thePeopleArray) { | |
| if rv.count == 1 { | |
| print("\nFound Exactly ONE match! \n") | |
| // returned "rv" is still an array, even if it was a single match | |
| if let person = rv.first { | |
| print(person) | |
| } | |
| } else { | |
| // just printing them out to debug console here... | |
| // you'll need to write a "pick which one" function | |
| print("\nMultiple Matches found... \n") | |
| for person in rv { | |
| print(person) | |
| } | |
| } | |
| } else { | |
| print("\n\t!!! Find People returned nil - ZERO matches found !!!!") | |
| } | |
| print("\n\n end") | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment