美文网首页
633,swift 中open,public,internal,

633,swift 中open,public,internal,

作者: 枫叶1234 | 来源:发表于2021-06-18 10:45 被阅读0次

    swift 中关于open ,public ,internal,fileprivate,private 修饰的说明

    都从类修饰和属性修饰

    open 修饰类 可以在其他模块中的类中修饰,在引用该模块的的方法中,可以继承
    在其他模块中的类的属性修饰,在引用该模块的方法中,可以重写属性
    public 修饰类 只能在本模块中被继承,如果是修饰了属性,只能在本模块中的子类中重写
    internal:在模块内部可以访问,但是不能在模块外部访问

    filePrivate:在同一个文件中可以访问

    Private:只能在一个类中访问

    mutating 在结构体中修改属性值

    open:

    用open修饰的类可以在本某块(sdk),或者其他引入本模块的(sdk,module)继承,如果是修饰属性的话可以被此模块或引入了此某块(sdk)的模块(sdk)所重写

    public:

    类用public(或级别更加等级更低的约束(如private等))修饰后只能在本模块(sdk)中被继承,如果public是修饰属性的话也是只能够被这个module(sdk)中的子类重写

    internal

    是在模块内部可以访问,在模块外部不可以访问,a belong A , B import A, A 可以访问 a, B 不可以访问a.比如你写了一个sdk。那么这个sdk中有些东西你是不希望外界去访问他,这时候你就需要internal这个关键字(我在导入第三方框架时发现其实没有定义的话sdk里面是默认internal的)

    fileprivate

    这个修饰跟名字的含义很像,file private 就是文件之间是private的关系,也就是在同一个source文件中还是可以访问的,但是在其他文件中就不可以访问了 a belong to file A, a not belong to file B , 在 file A 中 可以访问 a,在 file B不可以访问a

    private

    这个修饰约束性比fileprivate的约束性更大,private 作用于某个类,也就是说,对于 class A ,如果属性a是private的,那么除了A外其他地方都不能访问了(fileprivate 和private都是一种对某个类的限制性约束。fileprivate的适用场景可以是某个文件下的extension,如果你的类中的变量定义成了private那么这个变量在你这个类在这个类的文件的拓展中就无法访问了,这时就需要定义为fileprivate)

    最后是 Guiding Principle of Access Levels (访问级别的推导原则):不能在低级别的修饰中定义比自身更高的级别修饰,如public不能修饰在private类中的属性

    Swift中的Public和Open有什么区别?

    Today we’ll quickly explore the difference between the public and open keywords in Swift.

    今天,我们将快速探索Swift中public和open关键字之间的区别。

    In short, we use open when we’re working on a module that at some point will be imported in a different module and overridden.

    简而言之,当我们在某个模块上工作时,我们会使用open ,这有时会导入到另一个模块中并被覆盖。

    For example, when you’re developing your own reusable UI element and want to distribute it using CocoaPods, you may consider using the open keyword to let others override the behavior of your library. With public, you may simply import the framework, but you can’t override it.

    例如,当您开发自己的可重用UI元素并希望使用CocoaPods进行分发时,可以考虑使用open关键字让其他人重写您的库的行为。 使用public ,您可以简单地导入框架,但是不能覆盖它。

    Without a further ado, let’s get started.

    事不宜迟,让我们开始吧。

    开始吧 (Let’s Start)

    Say we’re creating our own UI element, and we want it to be reusable and independent. So we create a new target in our existing project, like so:

    假设我们正在创建自己的UI元素,并且希望它可重用且独立。 因此,我们在现有项目中创建了一个新目标,如下所示:

    image.png

    Next, we choose Static Library in the drop-down:

    接下来,我们在下拉列表中选择“静态库”:

    image.png

    We name it CircleView and create a simple rounded UIView:

    我们将其命名为CircleView并创建一个简单的圆形UIView :

    Now let’s import it into our main project. First, we need to go to the Build Settings and add a dependency:

    现在,将其导入到我们的主项目中。 首先,我们需要转到“构建设置”并添加一个依赖项:

    image.png

    Finally, we’re able to add the CircleView in our main target’s files. Let’s now try to create a custom subclass in the CustomCircleView.swift file:

    最后,我们可以在主要目标的文件中添加CircleView 。 现在,让我们尝试在CustomCircleView.swift文件中创建一个自定义子类:

    We can see that we want to override the color property of the CircleView. However, the following errors pop up:

    我们可以看到我们想覆盖CircleView的color属性。 但是,会弹出以下错误:

    image.png

    As errors imply, we must mark the CircleView class and properties as open to be able to inherit from it and override its properties. Let’s modify the CircleView.swift file:

    就像错误所暗示的那样,我们必须将CircleView类和属性标记为open ,以便能够从其继承并覆盖其属性。 让我们修改CircleView.swift文件:

    Now if we return to the main target and build the project, we’ll see no errors.

    现在,如果返回到主要目标并构建项目,则不会看到任何错误。

    Note that we can’t mark structs as open; it only applies to classes and class members.

    注意,我们不能将结构标记为open ; 它仅适用于班级和班级成员。

    结语 (Wrapping Up)

    相关文章

      网友评论

          本文标题:633,swift 中open,public,internal,

          本文链接:https://www.haomeiwen.com/subject/sfjkyltx.html