无法在另一个文件的扩展名中使用私有属性 [英] Can't use private property in extensions in another file

查看:121
本文介绍了无法在另一个文件的扩展名中使用私有属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法在扩展程序中使用私有财产.我的扩展名在另一个文件中.

I can't use private property in extension. My extension is in another file.

如何在扩展程序中使用私有财产?

How can I use private property in extension?

推荐答案

更新 从Swift 4开始,扩展可以访问在同一文件中声明的类型的私有属性.请参阅访问级别.

Update Starting with Swift 4, extensions can access the private properties of a type declared in the same file. See Access Levels.

如果属性声明为private,则其访问将仅限于封闭的声明以及该声明在同一文件中的扩展名.

If a property is declared private, its access is restricted to the enclosing declaration, and to extensions of that declaration that are in the same file.

如果将属性声明为fileprivate,则只能在声明的文件中使用该属性(以及该文件中的任何).

If the property is declared fileprivate, it can only be used within the file it is declared (and by anything in that file).

如果将该属性声明为internal(默认设置),则只能在声明该属性的模块(以及该文件中的任何)中使用该属性.

If the property is declared internal (the default), it can only be used within the module it is declared (and by anything in that file).

如果将该属性声明为publicopen,则导入该声明所在模块的文件可以在模块内部以及模块外部使用任何属性.

If the property is declared public or open, it can be used by anything within the module as well as outside of the module by files that import the module it is declared in.

这篇关于无法在另一个文件的扩展名中使用私有属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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