ios - The use of private keyword in Swift and their use in protocols? -


i'm building ios app want make protocol (which understanding equivalent of java interfaces) model, use unit testing purposes.

in java typically want encapsulate values in model , make them accessible through getters , setters only.

how can ensure encapsulation in swift protocols, can't use private keyword properties.

my model setup this:

class model {      private var property: int = 5      func getproperty() -> int {         return property     }      func setproperty(newvalue: int) {         self.property = newvalue     } } 

and want protocol this:

protocol modelprotocol {       private var property: int { set } } 

my problem can't declare properties private, thing in swifts access control (i've read have private, internal , public) don't use private properties or there equivalent java's way of handling interfaces , private variables?

(note: i'm using xcode 7 , swift 2.0 if matters)

i don't think can have private properties , public getters , setters swift code, getters , setters cannot have higher access control property. unit testing purposes can access internal entity using @testable attribute shown in documentation below.

access levels unit test targets

when write app unit test target, code in app needs made available module in order tested. default, entities marked public accessible other modules. however, unit test target can access internal entity, if mark import declaration product module @testable attribute , compile product module testing enabled.

i not think able declare private properties within protocol, may need use base class instead internal properties , extend class. still new protocols myself, believe used ensure code conforms protocol in... provides functionality or methods.

references used:

swift access control

swift properties

swift unit testing access


Comments

Popular posts from this blog

php - Invalid Cofiguration - yii\base\InvalidConfigException - Yii2 -

How to show in django cms breadcrumbs full path? -

ruby on rails - npm error: tunneling socket could not be established, cause=connect ETIMEDOUT -