前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Swift:为 String、Array、Dictionary 添加 isNotEmpty 属性

Swift:为 String、Array、Dictionary 添加 isNotEmpty 属性

作者头像
网罗开发
发布2021-12-07 14:05:09
5640
发布2021-12-07 14:05:09
举报
文章被收录于专栏:网罗开发网罗开发

前言

想要为 Swift 的 String、Array、Dictionary 这几种常见类型,添加一个 isNotEmpty 属性。

灵感来源于 Dart 中对于判断数组不为空有一个 isNotEmpty 属性:

代码语言:javascript
复制
final array = [1, 2, 3, 4];
print(array.isNotEmpty);

Dart 有,Swift 也可以有啊。

直接明了版本

最直接明了的版本当然就是分别给 String、Array、Dictionary 写分类,在分类中添加一个只读计算属性 isNotEmpty 即可。

String+Extension:

代码语言:javascript
复制
extension String {
    var isNotEmpty: Bool { !isEmpty }
}

Array+Extension:

代码语言:javascript
复制
extension Array {
    var isNotEmpty: Bool { !isEmpty }
}

Dictionary+Extension:

代码语言:javascript
复制
extension Dictionary {
    var isNotEmpty: Bool { !isEmpty }
}

上面 3 个分类,分别实现了 String、Array、Dictionary 三个常用类型的 isNotEmpty。

但是!!!

你要了解到,有 isEmpty 属性的类型远不止以上三种类型,难道之后有需求对其他带有 isEmpty 属性的类型添加 isNotEmpty 属性,我都要来写一个分类?

这很明显的是没有看透 String、Array、Dictionary 这些类型的背后,是由什么在支撑着它们可以拥有 isEmpty 属性。

更本质的版本

滴水穿石非一日之功,冰冻三尺非一日之寒。

我想说的是入门的时候都会选择直接了当的写法,不过当在反反复复 CV 代码的时候,我们需要的是观察,归纳,抽离,封装。

上图不知道大家注意到没有:

  • Dictionary → Collection
  • Array → MutableCollection → Collection

Array 与 Dictionary 都遵守了 Collection 协议,那么这个 isEmpyt 属性会不会就存在于 Collection 协议中呢?

带着这个疑问,我们去看看 Collection 协议就知道了:

代码语言:javascript
复制
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 }
}

上面这段代码,摘自于 Swift 中的 Collection 源码,如果仔细看代码注释,会发现,举例说明中是以 String 的 isEmpty 进行的,这也说明 String 类型直接或者间距都遵守 Collection 协议的。

这么一来就好办了,我只需要在 Collection 协议的分类中,添加一个 isNotEmpty 属性即可:

代码语言:javascript
复制
extension Collection {
    /// 判断集合非空
    public var isNotEmpty: Bool {
        return !isEmpty
    }
}

使用:

代码语言:javascript
复制
let array = []
print(array.isNotEmpty)

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

let string = ""
print(string.isNotEmpty)

以上代码均可以点出 isNotEmpty,并打印 true,效果符合预期。

Swift 里集合类型协议的关系[1]

总结

代码总是搬运不完的啦,但是读懂代码,观察代码我们都是可以的啦,只不过这其中需要我们多写一些代码,甚至多走一点弯路。

我们下期见。

参考资料

[1]

Swift 里集合类型协议的关系: https://www.cnblogs.com/huahuahu/p/Swift-li-ji-he-lei-xing-xie-yi-de-guan-xi.html

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2021-12-01,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 网罗开发 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 直接明了版本
  • 更本质的版本
  • 总结
    • 参考资料
    领券
    问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档