Swift Property Encapsulation -


i'm wondering "standard" way of declaring property in swift is. in particular, seems of apple's swift example code doesn't encapsulate properties (you can freely , set property without having go through getter , setter methods). (i'm used java have trouble believing that's how apple expects developers write code.)

right now, have:

private var x: int private var xlistener: xlistener? public func getx() -> int {     return x } public func setx(newvalue: int) {     x = newvalue     if (let listener = xlistener) {         listener.onxset(x)     } } 

but feel there must simpler way this. saw mention of computed properties seems don't store value, use them you'd have do:

private storedx: int public var x: int {     {         return storedx     }     set {         storedx = newvalue     }     didset {         if (let listener = xlistener) {             listener.onxset(storedx)         }     } } 

i have trouble believing either of these methods "right" way since they're complicated. swift not typically encapsulate properties, or missing something?

a setter , getter nothing provide no encapsulation @ vs using property. there's literally no semantic difference between this:

private storedx: int public var x: int {     {         return storedx     }     set {         storedx = newvalue     } } 

and this

public var x: int 

you can go , change (albeit limitation have recompile linked code, that's true in both cases now. fixed in swift 3) x computed property later, or add didset/willset, etc...

can explain in more detail you're seeking gain encapsulating x setter , getter same thing setting , getting directly?

(edit: directly answer question though, this:

public var x : int {     didset {         xlistener?.onxset(storedx)     } } 

)


Comments

Popular posts from this blog

c - How to retrieve a variable from the Apache configuration inside the module? -

c# - Constructor arguments cannot be passed for interface mocks -

python - malformed header from script index.py Bad header -