0

I am new to swift (but not new to coding), so I am a little bit cofused with this. I use Kanna but if I have two XMLElements node1 and node2 and I write:

let result = node1 === node2

I get "Binary operator === cannot be applied to two 'XMLElement' operands". Is this comparison possible with some other syntax, and if it is impossible, then why and what is the best workaround (I can think of comparing innerHTML it would work in nearly all cases but seems inefficient)?

Ivan Ičin
  • 9,672
  • 5
  • 36
  • 57
  • What is XMLElement? What are you trying to achieve? – jtbandes Oct 10 '15 at 18:53
  • I've wrote above, it is defined in Kanna library for swift. I want to see if those two elements returned from two xpath queries are the same. It works like that in C#. – Ivan Ičin Oct 10 '15 at 18:55
  • You have to use '==' to compare not '===' and you can't declare variable like this you have to use if statement for comparing, inside that you can do anything. – Mukesh Thawani Oct 10 '15 at 19:00
  • @MukeshThawani, I can store result in Bool and declare it as above, yes it is usually useless, but this is just an example. You can't compare it with '==', and you haven't written why you can't compare with '===', as according to everything I read about that operator it should work. – Ivan Ičin Oct 10 '15 at 19:02
  • In swift there is nothing like this '==='. You can read about operators in swift here [link](https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/BasicOperators.html) – Mukesh Thawani Oct 10 '15 at 19:04
  • @MukeshThawani, thanks for trying to help, but this is for some more experienced coders. There is such an operator, read it here: http://stackoverflow.com/questions/24002819/difference-between-and – Ivan Ičin Oct 10 '15 at 19:06
  • If there was no such operator, I would get error that there is no such operator, not that it can't be applied on specific objects. – Ivan Ičin Oct 10 '15 at 19:10
  • 1
    @IvanIčin you are correct, I am sorry there was nothing like this in official documentation that's why I thought. – Mukesh Thawani Oct 10 '15 at 19:19

1 Answers1

1

XMLElement(from Kanna) is a protocol not a class and it's not conform to Comparable/Equatable protocol responsible for comparisons http://mgrebenets.github.io/swift/2015/06/21/equatable-nsobject-with-swift-2/

The '===' check if two object references refer to the same object instance. This is done by comparing the addresses of the objects, not values. You can have the same functional as '===' using the code below::

func isIdentityObject(firstObject:XMLElement, withObject:XMLElement) -> Bool {
    let p1 = unsafeAddressOf(firstObject as! AnyObject)
    let p2 = unsafeAddressOf(withObject as! AnyObject)
    return (p1 == p2)
}

...

let result = isIdentityObject(node1, withObject: node2)
Iurie Manea
  • 1,217
  • 10
  • 16
  • Thanks for the answer, this seems promising, will check it tomorrow and mark it as the answer if it is OK. – Ivan Ičin Oct 10 '15 at 23:17
  • This doesn't seem to work, I try: let allnodes = doc?.xpath("descendant::node()") let h1node = doc?.xpath("//h1")[0] for node in allnodes! { if self.isIdentityObject(node, withObject: h1node!) { break } } It never breaks, though it enters the loop and document has h1 node (and I think it wouldn't enter the loop unless it had it, it would raise exception). – Ivan Ičin Oct 11 '15 at 11:59
  • My code works 100% and is the answer to your question, but I think you put an "incorrect" question for your problem and of course my answer not solve your problem :) I think you go in the wrong direction and you do not need '==='. The operator '===' in Swift is not equivalent to '===' in PHP, it compares only the memory address and even if two objects have the same values ​​but different instances, that points to two different memory addresses, the result will be false. You need to compare the elements by the values/properties not by memory addresses. – Iurie Manea Oct 11 '15 at 12:59
  • OK, so two xpaths that return the same node don't return the same node actually? It ain't like that in other languages like C#, but if it is like that, you are right, I have already written that I currently do by comparing innerHTML properties, just this isn't 100% right and is inefficient comparing to my C# solution, but if there is no other way and no one gives any new info, I'll mark your answer as correct. – Ivan Ičin Oct 11 '15 at 13:03