Search code examples
arraysswiftsetintersectionhashable

Different results with Set altough similar code? What is the excact problem in Swift?


i have a small problem. I have two Arrays where i try to find the same content in it. So i decided to convert it to a Set and then using those nice functions with "subtract". However i get very different results. Can someone tell me why this happens? When i use "subtracting" instead of "subtract" i get no problems however this is very weird for me and i really have no clue why this happens.

   var objectIDsWhichExist = [ "kjugsJHL6JYoByOreUQ0wUefsbX2", "18ixZ21PJDXA1WzeJqZzctl7tTk2", "ZeQPYGfDvWMLSVykb4M5FQ6miGX2"]
    var helperObjectIDsWhichExistInAdded = [ "kjugsJHL6JYoByOreUQ0wUefsbX2", "18ixZ21PJDXA1WzeJqZzctl7tTk2"]
    


    var setA = Set(objectIDsWhichExist) /* Updated Data*/
    var setB = Set(helperObjectIDsWhichExistInAdded) /* Standard Data*/
    let different = setA.subtract(setB) // I GET HERE ()  
    print(different) // I GET THIS RESULT "()\n"

And this is the one sample which works, surprisingly. However i still dont know really why???

    var employees: Set = Set(objectIDsWhichExist)
    let neighbors: Set = Set(helperObjectIDsWhichExistInAdded)
    employees.subtract(neighbors)
    print(employees) // HERE IT DOES WORK because i get this -> ["ZeQPYGfDvWMLSVykb4M5FQ6miGX2"]\n"

Solution

  • subtract and subtracting both computes the difference of 2 sets, but subtract is mutating, whereas subtracting is not.

    This means that x.subtract(y) changes the set x to the computed difference, whereas x.subtracting(y) doesn't change x at all, and instead returns the difference. On the other hand, subtract returns nothing (Void).

    When you do

    let different = setA.subtract(setB) // I GET HERE ()  
    print(different)
    

    you see () being printed, because that is what the string representation of Void looks like - an empty tuple.

    This works

    employees.subtract(neighbors)
    print(employees)
    

    because subtract changes employee.

    This also works:

    let different = setA.subtracting(setB)
    print(different)
    

    because the return value of subtracting - the set difference - is assigned to different. Note that this doesn't change setA.

    This doesn't work:

    employees.subtracting(neighbors)
    print(employees) // still shows the original employees
    

    Because subtracting doesn't change employees, and you are ignoring its return value.

    There are many other pairs of such mutating vs non-mutating methods, like

    • Set.formUnion vs Set.union
    • String.append vs String.appending

    Related post