Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
175 views
in Technique[技术] by (71.8m points)

arrays - 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 "()
"

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"]
"
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...