Reputation: 535989
Try this:
let url1 = NSURL(string: "foo bar")
let url2 = NSURL(string: "http://www.apple.com")
var arr1 = NSURL[]()
var arr2 = NSURL[]()
// one order
arr1 += url1
arr1 += url2
// other order
arr2 += url2
arr2 += url1
// equality test
println(arr1 == arr2) // true!?
After some experimentation, I think this has to do with Optionals. url1
comes back as nil, so to form an array of both urls, Swift has to treat these arrays (I'm guessing) as arrays of Optional<NSURL>
. But I still think it's a bug; it's as if the notion of comparing an array of optional NSURLs causes Swift to throw up its hands somehow... And, one might argue, it's a bug in another sense too, because I explicitly typed these arrays as NSURL[]
; I didn't say NSURL?[]
, so why is Swift permitting an Optional to go into them in the first place?
I'm wondering if anyone has noticed anything similar that might throw light on the matter. (I don't think this post is related, though of course I could be wrong.)
EDIT Some people have expressed an inability to reproduce the issue, so I've posted an example project at https://github.com/mattneub/SwiftArrayEqualityTest
Upvotes: 1
Views: 686
Reputation: 93296
In fact, all instances from the Objective-C APIs (essentially any UI* or NS* class) are actually optional, despite not appearing to be so via declaration, since Objective-C types can always be nil
. An array of NSURL
is essentially an array of implicitly unwrapped optional NSURL
s, or NSURL![]
.
(I explained more in this answer.)
So it isn't a bug that the array can take a nil
value, but it is a bug that the two evaluate to equal. Get thee to bugreport.apple.com.
Upvotes: 3