Small knowledge, big challenge! This paper is participating in theEssentials for programmers”Creative activities.

preface

We want to add an isNotEmpty attribute for the common Swift String, Array, and Dictionary types.

The inspiration comes from the isNotEmpty property in Dart that determines the array isNotEmpty:

final array = [1, 2, 3, 4];
print(array.isNotEmpty);
Copy the code

Dart has it, Swift can have it.

Straightforward version

The most straightforward version would be to classify String, Array, and Dictionary separately by adding a read-only calculation attribute isNotEmpty to the category.

String+Extension:

extension String { var isNotEmpty: Bool { ! isEmpty } }Copy the code

Array+Extension:

extension Array { var isNotEmpty: Bool { ! isEmpty } }Copy the code

Dictionary+Extension:

extension Dictionary { var isNotEmpty: Bool { ! isEmpty } }Copy the code

The above three categories implement isNotEmpty of String, Array, and Dictionary, respectively.

But!!

You have to understand that there are many more types that have isEmpty attributes than the above three. Do I need to write a classification for any other type that has an isNotEmpty attribute?

It’s obvious that you don’t see behind the strings, arrays, and dictionaries that they can have an isEmpty property.

A more essential version:

Rome was not built in a day.

What I want to say is that when you get started you write it straight, but when you go back and forth with CV code, you need to look, you need to generalize, you need to extract, you need to encapsulate.

I don’t know if you noticed:

  • The Dictionary to Collection

  • Array → MutableCollection → Collection

Both Array and Dictionary comply with the Collection protocolisEmpytDo attributes exist in the Collection protocol?

With this in mind, let’s take a look at the Collection protocol:

extension Collection { /// A Boolean value indicating whether the collection is empty. /// /// When you need to check whether your collection is empty, use the /// `isEmpty` property instead of checking that the `count` property is /// equal to zero. For collections that don't conform to /// `RandomAccessCollection`, accessing the `count` property iterates /// through the elements of the collection. /// /// let horseName = "Silver" ///  if horseName.isEmpty { /// print("My horse has no name.") /// } else { /// print("Hi ho, \(horseName)!" ) /// } /// // Prints "Hi ho, Silver!" ) /// /// - Complexity: O(1) @inlinable public var isEmpty: Bool { get } }Copy the code

The above code, taken from the Collection source in Swift, remains untouched. If you look closely at the code comments, you will see that the example is in isEmpty of String, which also shows that String is compliant with the Collection protocol.

To do this, I just need to add an isNotEmpty attribute to the Collection protocol’s classification:

Public var isNotEmpty: Bool {return! isEmpty } }Copy the code

Use:

let array = []
print(array.isNotEmpty)

let dict = [:]
print(dict.isNotEmpty)

let string = ""
print(string.isNotEmpty)
Copy the code

All of the above can be clicked isNotEmpty and printed true.

Reference Documents:

Set type protocol relationship in Swift

conclusion

The code is always handling, but to read the code, we can observe the code, but it requires us to write some more code, and even take a little more detours.

We’ll see you next time.