如何在Swift中正确使用类扩展? [英] How to properly use class extensions in Swift?

查看:125
本文介绍了如何在Swift中正确使用类扩展?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Swift中,我历史上使用扩展来扩展封闭类型并提供方便,无逻辑的功能,如动画,数学扩展等。但是,由于扩展是硬依赖性遍布代码库,我总是认为三在实现某些内容作为扩展之前的时间。

In Swift, I have historically used extensions to extend closed types and provide handy, logic-less functionality, like animations, math extensions etc. However, since extensions are hard dependencies sprinkled all over your code-base, I always think three times before implementing something as an extension.

尽管如此,我已经看到Apple建议在更大程度上使用扩展,例如将协议实现为单独的扩展。

Lately, though, I have seen that Apple suggests using extensions to an even greater extent, e.g. implementing protocols as separate extensions.

也就是说,如果你有一个实现协议B的A类,你最终得到这个设计:

That is, if you have a class A that implement protocol B, you end up with this design:

class A {
    // Initializers, stored properties etc.
}

extension A: B {
    // Protocol implementation
}

当你进入兔子洞时,我开始看到更多基于扩展程序的代码,例如:

As you enter that rabbit-hole, I started seeing more extension-based code, like:

fileprivate extension A {
    // Private, calculated properties
}

fileprivate extension A {
    // Private functions
}

我的一部分就像你在单独的扩展中实现协议时得到的构建块一样。它使得该类的独立部分非常独特。但是,只要继承此类,就必须更改此设计,因为无法覆盖扩展函数。

One part of me like the building-blocks you get when you implement protocols in separate extensions. It makes the separate parts of the class really distinct. However, as soon as you inherit this class, you will have to change this design, since extension functions cannot be overridden.

我认为第二种方法是......有趣。一旦它很棒,你不需要注释每个私有属性并作为私有属性,因为你可以为扩展指定。

I think the second approach is...interesting. Once great thing with it is that you do not have to annotate each private property and function as private, since you can specify that for the extension.

然而,这个设计也拆分存储和非存储的属性,公共和私有函数,使类的逻辑更难遵循(写小类,我知道)。这与子类化问题一起使我在扩展仙境的门廊上停了一下。

However, this design also splits up stored and non-stored properties, public and private functions, making the "logic" of the class harder to follow (write smaller classes, I know). That, together with the subclassing issues, makes me halt a bit on the porch of extension wonderland.

很想听听世界上Swift社区如何看待扩展。你怎么看?是否有一个银饰?

Would love to hear how the Swift community of the world looks at extensions. What do you think? Is there a silverbullet?

推荐答案

这只是我的意见,当然,所以我要写的很容易。

This is only my opinion, of course, so take what I'll write easy.

我目前在我的项目中使用扩展方法,原因如下:

I'm currently using the extension-approach in my projects for few reasons:


  • 代码非常干净:我的类永远不会超过150行,通过扩展分离使我的代码更具可读性并被责任分开

  • The code is much clean: my classes are never over 150 lines and the separation through extensions makes my code more readable and separated by responsibilities

这通常是一个类的样子:

This is usually what a class looks like:

final class A {
    // Here the public and private stored properties
}

extension A {
    // Here the public methods and public non-stored properties
}

fileprivate extension A {
    // here my private methods
}

扩展名可以不止一个,当然,这取决于你班级的作用。这对于组织代码并从Xcode顶部栏中读取它非常有用

The extensions can be more than one, of course, it depends on what your class does. This is simply useful to organize your code and read it from the Xcode top bar


  • 它提醒我 Swift 是一种面向协议的编程语言,而不是OOP语言。协议和协议扩展没有任何功能。我更喜欢使用协议为我的classes / struct添加安全层。例如,我通常以这种方式编写模型:

  • It reminds me that Swift is a protocol-oriented-programming language, not an OOP language. There is nothing you can't do with protocol and protocol extensions. And I prefer to use protocols for adding a security layer to my classes / struct. For example I usually write my models in this way:

protocol User {
    var uid: String { get }
    var name: String { get }
}

final class UserModel: User {
    var uid: String
    var name: String

    init(uid: String, name: String) {
        self.uid = uid
        self.name = name
    }
}


通过这种方式,您仍然可以编辑 uid 名称 UserModel 类中的值,但你不能在外面,因为你只会处理用户协议类型。

In this way you can still edit your uid and name values inside the UserModel class, but you can't outside since you'll only handle the User protocol type.

这篇关于如何在Swift中正确使用类扩展?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆