0

Using ReactiveSwift, I've used the following code:

let alertMessageProperty = MutableProperty<String?>(nil)
...
.on(failed: { self.alertMessageProperty.value = $0.localizedDescription })
...

which means:

  • self.alertMessageProperty.value is of type String?
  • in .on(failed:), failed is of type ((Error) -> Void)? so $0 is of type Error and $0.localizedDescription is of type String.
  • in a simple Playground, assigning a String to a String? variable works fine, as expected.

It does not compile and I get this error:

'String' is not convertible to 'String?'

I think String is SO convertible to String?. What's going on here?

Versions: Swift 3.2 - ReactiveSwift 2.0.1

Mick F
  • 7,312
  • 6
  • 51
  • 98
  • Possible duplicate of [Error: 'String' is not convertible to 'String!'](https://stackoverflow.com/questions/38186121/error-string-is-not-convertible-to-string) – alseether Oct 19 '17 at 12:27
  • @alseether: Interesting but the answer in that post is not clear. It sounds like a Swift compiler bug but it is not clearly mentioned as is. – Mick F Oct 19 '17 at 12:31
  • @LinusGeffarth: assigning a String? value to a String variable can't work but the other way around must. Your comment is elusive and not helpful. – Mick F Oct 19 '17 at 12:33
  • Please clarify a few things... which version of Swift? what are the types of `self.alertMessageProperty.value` & `$0.localizedDescription`? Can be reproduced out of context by trying an assignement on same types? – Alladinian Oct 19 '17 at 12:49
  • @Alladinian: edited to clarify things – Mick F Oct 19 '17 at 13:01
  • I can't seem to reproduce it, is there any way you can create a small example that we can try out like @Alladinian suggested – TNguyen Oct 19 '17 at 13:41

1 Answers1

2

There is a lot missing in your example so I can not reproduce the exact problem.

Your code that you left out in ... is probably a lot more complicated, maybe a chain of multiple reactive operations, and my guess is that the actual error is something completely different somewhere in that code. The error message you're getting is just misleading due to some bug i the Swift compiler.

You can try to break up your code into smaller pieces and see if the compiler now fails at a different place and try to narrow it down that way.

MeXx
  • 3,357
  • 24
  • 39