Home > OS >  How to make a property only readable in a swift package while can be accessed outer of it?
How to make a property only readable in a swift package while can be accessed outer of it?

Time:08-03

I'm separating a class from my main project and implement it as a module in a swift package. So I create a swift package and paste my code into it. Then, add it as a local package in my main project.

My original code makes my properties private(set), which is intended to make it readable and not settable.

// Original code - properties of a class

@Published private(set) var lines: [String] = []
@Published private(set) var index: Int = -1

But it reports an error 'index' is inaccessible due to 'internal' protection level. So I assume I have to change the private(set) into public. But it cannot protect it from editing outside of the class. How can I solve it?

CodePudding user response:

Every property has a combination of accessibilities for get and set.

The default is

internal internal(set)

You seem to want

public private(set)
  • Related