Skyscanner / SkyFloatingLabelTextField

A beautiful and flexible text field control implementation of "Float Label Pattern". Written in Swift.
Apache License 2.0
4.09k stars 540 forks source link

Fix crash caused by nil lineView and titleLabel #216

Closed alextov closed 6 years ago

alextov commented 6 years ago

When creating SkyFloatingLabelTextField from storyboard and overriding certain properties, it is possible to get an Unexpectedly found nil while unwrapping an Optional value fatal error in updateLineColor() and some other methods.

Here is a minimal working example:

import SkyFloatingLabelTextField
import UIKit

final class ViewController: UIViewController {

    @IBOutlet private weak var textField: SkyFloatingLabelTextField!

}

extension SkyFloatingLabelTextField {

    override open var isSecureTextEntry: Bool {
        didSet {
            textColor = isSecureTextEntry ? .blue : .red
        }
    }

}

(Full Xcode project.)

The proposed pull request solves this issue by performing optional unwrapping with guards.

I didn’t check all places where implicit option unwrapping occurs, so there may be other crashes left related to this issue. To properly address it I was thinking to rewrite the component to have pairs of optional and non-optional properties (e.g., optional customLineView and non-optional lineView). That way user will still be able to customize the respective views and there will be a reliable fallback option. It is out of scope of this pull request, but if you deem it useful I can explore it further and prepare a separate proposal.

k0nserv commented 6 years ago

Thanks @alextov this looks good, can you add a line to CHANGELOG.md crediting yourself too?

alextov commented 6 years ago

@k0nserv Done.