jax jax - 5 months ago 10x
Swift Question

swift @property directive : custom accessors

As I referred here, there is no @property or @synthesise directive present in swift.


1] how the setter and getter will be added by compiler?

2] If I have a object, how to write our own setter and getter methods?

I am new to Can any one explain with example?

//EDIT :

Before down voting, please let me show as in the Apple documentation.

@property (getter=isFinished) BOOL finished;
will override getter method.

In the same way how can I achieve in Swift?

For in the below example, how to make getter as

class CarPaint : NSObject {

var finished:Bool = Bool();

func getCarDoorColor() -> UIColor {

if finished { //instead here I want to `isFinished`
return UIColor.whiteColor()

return UIColor.brownColor()




Answering the 2nd part your of question, there is no named getter in Swift till now.

@property (getter=isFinished) BOOL finished;

Apple suggests the above Objective-C equivalent code in Swift in their migration documentation

var _finished:Bool = false // finished boolean variable.

var finished:Bool { //Getter(custom) and Setter for finished variable.

    @objc(isFinished) get {
        return self._finished
        _finished = newValue

Only way to make work around of getter self.isFinished()

func isFinished() ->  Bool {
    return _finished;

I hope you got the solution.