Why rex_text for UILabel returns not optional string
Igor-Palaguta opened this issue · comments
Hello,
UILabel has var text: String?
Why rex_text does not have MutableProperty<String?>
type?
currently have to write in many places label.rex_text <~ optionalText.map { $0 ?? "" }
Thanks!
Good point, we would need to make a new major release for this change tho.
We're still on 0.x releases so we can safely change this in 0.x+1 and be semver compliant.
I'm not a fan of this change, even when ignoring the compilation errors I now get everywhere, this feels like a step backwards away from the "Swift way".
I found the type being a non optional String
a feature rather than a bug. When setting the text
property there's no difference between nil
or an empty String
, both have the same result, being an empty UILabel
. Hence MutableProperty<String>
simplified the use. Now, when testing whether a UILabel
is empty, we have to test whether the value is nil
and whether it's an empty String
.
Logically, an empty UILabel
contains an empty String
, the nil situation is/was an old Objective-C side-effect. Imho.
For instance a ViewModel exposes a username property of type MutableProperty<String>
, this used to match with rex_text
without having to add map's everywhere and manually handling the nil
situation.
Update 8/1/16: Changed UITextField
to UILabel
which I had mixed up. Issue was about UILabel
to start with.
@ynnckcmprnl we have tried to be coherent with what the different APIs expose and respect it. In this particular case, having a new look at it, I kinda agree with you. @dmcrodrigues what do you think?
@ynnckcmprnl thanks for your feedback and I agree in reverting this. To ground this decision there's a discussion in the documentation that clearly states:
This string is
@""
by default.
I also have done a few tests and even when you clear a text field with a nil
value you always receive an empty string while accessing text
property.
@ynnckcmprnl are you interested in contributing this change?
Note however that UILabel
, which was the issue stated here, should remain an optional string because it's nil
by default as we can see in the documentation.
Darn, I seem to have mixed up UILabel
& UITextField
. My issue was indeed with UILabel
though, the post should have said UILabel
instead of UITextField
. I'll update the post to prevent further confusion.
I still stand by my original opinion, but the documentation stating nil
as default value does make it a hard sell.
Good catch regarding UITextField
, the change to MutableProperty<String>
should be inarguable.
@ynnckcmprnl I don't think we should change this for UILabel
, the documentation clearly states that by default it's nil
so it's not case where the API exposed is not coherent but a case where we are making an assumption.
One thing we can do to help with this change is cherry-picking the support for binding with optionals.
struct ViewModel {
let name: MutableProperty<String>
}
// This will be possible without any mapping
nameLabel.rex_text <~ viewModel.name
This will help? @RuiAAPeres what do you think about this?
@dmcrodrigues That would be exactly what I need.
I wish that was included in RAC 4.x but unfortunately it will become available only in RAC5. Until then, I think we can provide it through Rex but I'll ask the opinion of @neilpa and @RuiAAPeres about this.
@ynnckcmprnl kinda wasted effort to add this to Rex and just remove it again later on. It might be preferable to add this on your own codebase as a small extension and remove it, once you adopt RAC5.