简体   繁体   中英

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

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?

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

The default is

internal internal(set)

You seem to want

public private(set)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM