1

I have two collections of phone numbers that I want to compare to see if any match. In other languages, I'd loop through one collection, add it to a collection var type that requires uniqueness, loop through the other and check for matches such as:

var phones = ["1","2","3"]
var phones2 = ["2","5","6"]
var uniqueCollection: Set = Set()
for var i = 0; i < phones.count; i++ {
    if (uniqueCollection.containsKey(phones[i]) == false){
        uniqueCollection.add(phones[i])
    }
}
var anyMatch = false
for var j = 0; j < phones2.count; j++{
    if uniqueCollection.containsKey(phones2[j]) {
        anyMatch = true
    }
}

So far I haven't found any way to do this as Swift Maps seem to be a transform, Dictionaries require a value to go with the keys, and don't have an explicit "containsKey()" type function, and it doesn't seem there's another collection like "a hash table" with a method to see if a var is in there. http://www.weheartswift.com/higher-order-functions-map-filter-reduce-and-more/

http://nshipster.com/swift-comparison-protocols/

Assuming this doesn't exist, I'm planning to just go the long way of double loops, which will suck for performance if there's ever two large collections.

func checkMultisAnyMatch(personMultis: [[AnyObject]], parseMultis: [[AnyObject]]) -> Bool{
    //Put all the phone #'s or emails for the person into an Array
    //Put all the phone #'s or emails for the PF contact into an array
    //Loop through the phones in the parseContact
    //if any match, break the loop, and set anyPhoneMatch = true
    var anyMatch = false
    for var i = 0; i < personMultis.count; i++ {
        //order is Id, label, value, type
        //that means it's in the 3rd column, or #2 subscript
        var personMulti:AnyObject? = personMultis[i][2]
        if (personMulti != nil) {
            for var j = 0; j < parseMultis.count; j++ {
                //order is Id, label, value, type
                var parseMulti:AnyObject? = parseMultis[j][2]
                if parseMulti != nil {
                    if parseMulti! as NSString == personMulti! as NSString {
                        anyMatch = true
                    }//if 4
                }//if 3
            }//for 2
        }//if
    }//for
    return anyMatch
}
John Kucera
  • 314
  • 3
  • 15
  • Compare also [How to create array of unique object list in Swift](http://stackoverflow.com/questions/24044190/how-to-create-array-of-unique-object-list-in-swift). Some of the answers show how to implement a set type in pure Swift. – Martin R Nov 09 '14 at 00:22
  • 1
    Swift 1.2 has a native Set type now, compare http://stackoverflow.com/a/28426765/1187415. – Martin R Feb 10 '15 at 08:28

1 Answers1

2

Would NSSet work for you?

func intersectsSet(_ otherSet: NSSet) -> Bool
Returns a Boolean value that indicates whether at least one object in the receiving set is also present in another given set.

You can create an NSSet from an NSArray.

var set1 = NSSet(array:["number1", "number2", "number3"])
var set2 = NSSet(array:["number4", "number2", "number5"])
var set3 = NSSet(array:["number4", "number5", "number6"])


let contains1 = set1.intersectsSet(set2) // true
let contains2 = set1.intersectsSet(set3) // false
zaph
  • 111,848
  • 21
  • 189
  • 228