ios - Swift access control on protocol conformance -
i have private protocol defined in file below
private protocol testprotocol { func testfunc1() func testfunc2() } a public class conforms above protocol follows
public class testclass : testprotocol { func testfunc1() {} func testfunc2() {} } as per apples documentation , members of public class internal access control default unless explicitly set different access control modifier.
the documentation says type's conformance protocol lower access control make type's implementation of protocol access control same of protocol. in scenario since type's access control public , protocols access control private , methods testfunc1 , testfunc2 should access control of private.
when class instantiated in different source file , methods accessed below , compiler not show error not expected methods should private per guidelines
var test: testclass = testclass() test.testfunc1() is expected behavior ? missing something?
apple documentation says:
when write or extend type conform protocol, must ensure type’s implementation of each protocol requirement has @ least same access level type’s conformance protocol.
according assume implementing methods testfunc1 , testfunc2 access control modifier inside testclass overrides protocol. if use default protocol implementation of methods following compiler return error:
extension testprotocol { func testfunc1() {} func testfunc2() {} } as far swift protocol oriented language replacing inheritance protocols it's reasonable if want change protocol defined access level of function inside custom class.
Comments
Post a Comment