When to use fileprivate access modifier in Swift?
When to use fileprivate access modifier in Swift?
In Swift, the fileprivate access modifier is used to restrict the visibility of an entity to its own defining source file. This means that any code within the same file can access the fileprivate entity, but it is not accessible from other files. Here are some scenarios where you might use fileprivate:
Multiple Types in One File: When you have multiple classes, structs, or extensions within the same file that need to interact with each other but should not be accessible from outside the file. For example, if you have a helper class or struct that is only relevant within the context of the file, you can use fileprivate to ensure it is not exposed outside.
final class ImageViewController: UIViewController {
fileprivate var imageView: UIImageView!
}
struct ImageProvider {
let newImage: UIImage
func updateImage(in viewController: ImageViewController) {
// As we used fileprivate, we can now access the imageView property.
viewController.imageView.image = newImage
Đăng nhập để tham gia thảo luận cùng mọi người.
Chưa có bình luận nào
Hãy là người đầu tiên chia sẻ suy nghĩ của bạn.