I am asking a question regarding Combines .first(where: )
method. When looking at my code, I use Hello World as the reference by which to check if it contains the values emitted by my publisher.
Why is H the first value thats emitted, when in reality O gets printed first?
I hope my question is not confusing. If you read the code it should make much more sense. Thanks!
// 1 Create a publisher that emits four letters.
let publisher = ["J", "O", "H", "N"].publisher
// 2 Use the first(where:) operator to find the first letter contained in Hello World and then print it out.
publisher
.print("publisher")
.first(where: { "Hello World".contains($0) })
.sink(receiveValue: { print("First match is \($0)") })
.store(in: &subscriptions)
And this is what get's printed to the console:
publisher: receive subscription: (["J", "O", "H", "N"])
publisher: request unlimited
publisher: receive value: (J)
publisher: receive value: (O)
publisher: receive value: (H)
publisher: receive cancel First match is H
Shouldn't the first match be O? Since it's hello world? I understand it also contains hello, but o get's emitted before h.
Thanks once more.
This has nothing to do with Combine, so all that scaffolding just boils down to:
print("Hello World".contains("J")) // false
print("Hello World".contains("O")) // false
print("Hello World".contains("H")) // true
print("Hello World".contains("N")) // false
Which just happens because "o" != "O"
and "H" == "H"
. That is, character equality is sensitive to capitalization.