[Swift] UITextField taught me the difference between nil and ""

The other day I witnessed the difference between nil and "" (from the letter), so I will record it.

What happened

UITextField (textField) for entering a message and a button (sendButton) for sending it Was implemented. スクリーンショット 2020-11-03 18.56.09.png

Here, it is meaningless if the textField does not have a value, so I thought about setting the submit button so that it can be used only when the textField has a value. The implementation is as follows


    @objc func textFieldDidChange(sender: UITextField) {
        if textField.text == nil {//Pay attention to this line
            sendButton.isEnabled = false
        } else {
            sendButton.isEnabled = true
        }
    }

textFieldDidChange () is a method that is called every time the value of textField changes. Let's check the execution result.

initial state

スクリーンショット 2020-11-03 19.08.09.png

Of course, you can't press the submit button because there is no value in textField.

Enter characters

スクリーンショット 2020-11-03 18.56.09.png

Characters are entered and the send button can be pressed.

Delete characters

スクリーンショット 2020-11-03 19.19.49.png

The send button can be pressed even though there are no characters.

: frowning2: Nande? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? : frowning2:

Workaround

From the above events, it was found that ** if you enter a character once and then delete it, it is not nil **. I changed the conditional expression thinking "Why was it written as textField.text == nil in the first place? "

    @objc func textFieldDidChange(sender: UITextField) {
        if commentTextField.text == ""{//Pay attention to this line
            sendButton.isEnabled = false
        } else {
            sendButton.isEnabled = true
        }
    }

This time, I changed "" (from the character) to nil in the conditional expression and put it in. Check the execution result.

Delete after entering characters

スクリーンショット 2020-11-03 19.39.46.png

It is difficult to convey with the image alone, but we were able to confirm normal operation.

Summary

Regarding UITextField, the handling of conditional expressions and elements corresponds as follows.

== nil == ""
initial state true true
Enter characters false false
Delete the entered characters false true

After this experience

This event gave me a little better understanding of the difference between nil and "". Until now, I was often told in books that "nil and" "are different !!", and I remembered myself, but this was the first time I had witnessed it in implementation.

Recognition of the current nil and ""

nil Completely innocent with no value in it! Insanely pure! !! Natural water Super beautiful

"" (character from)

An image that looks like "a state without any value" and artificially reproduces "a state without a value" Feeling that sewage is filtered and brought to nature A little dirty


If you have any problems with this recognition, I will write an article again.

Recommended Posts

[Swift] UITextField taught me the difference between nil and ""
Understand the difference between each_with_index and each.with_index
About the difference between irb and pry
[Java] Understand the difference between List and Set
[Rails / ActiveRecord] About the difference between create and create!
Understand the difference between abstract classes and interfaces!
What is the difference between SimpleDateFormat and DateTimeFormatter? ??
Difference between vh and%
Difference between i ++ and ++ i
[Ruby] I thought about the difference between each_with_index and each.with_index
[Rails] I learned about the difference between resources and resources
What is the difference between a class and a struct? ?? ??
What is the difference between System Spec and Feature Spec?
About the difference between classes and instances in Ruby
[Rails] What is the difference between redirect and render?
Compare the difference between dockerfile before and after docker-slim
What is the difference between skip and pending? [RSpec]
[Rails] I investigated the difference between redirect_to and render.
What is the difference between Java EE and Jakarta EE?
[Java] Difference between == and equals
Rails: Difference between resources and resources
Difference between puts and print
Difference between CUI and GUI
Difference between variables and instance variables
Difference between mockito-core and mockito-all
Difference between bundle and bundle install
Difference between render and redirect_to
Difference between List and ArrayList
Difference between .bashrc and .bash_profile
Difference between StringBuilder and StringBuffer
Difference between render and redirect_to
Difference between render and redirect_to
[Rails] What is the difference between bundle install and bundle update?
Difference between Java and JavaScript (how to find the average)
About the difference between "(double quotation)" and "single quotation" in Ruby
What is the difference between an action and an instance method?
[Java] Check the difference between orElse and orElseGet with IntStream
Let's override the difference between == (identity) and equals method (equivalence)
The difference between programming with Ruby classes and programming without it
[Ruby] Difference between get and post
Difference between instance method and class method
Difference between render method and redirect_to
Find the difference between List types
Difference between == operator and equals method
[Java] Difference between Hashmap and HashTable
About the difference between gets and gets.chomp (other than line breaks)
[Terminal] Difference between irb and pry
JavaServlet: Difference between executeQuery and executeUpdate
[Ruby] Difference between is_a? And instance_of?
Difference between == operator and eqals method
[Ruby] Difference between symbol variables and character string variables. About the difference between [: a] and ['a'].
Rough difference between RSpec and minitest
[Rails] Difference between find and find_by
Difference between instance variable and class variable
[JAVA] Difference between abstract and interface
Difference between Thymeleaf @RestController and @Controller
Difference between Stream map and flatMap
[Java] Difference between array and ArrayList
Difference between primitive type and reference type
[Java] What is the difference between form, entity and dto? [Bean]
Difference between string.getByte () and Hex.decodeHex (string.toCharaArray ())