After reading @property/@synthesize equivalent in swift, I became curious for how the functionality from Objective C was retained.
Say I have the following in Objective C:
@interface MyClass
@property (readwrite) MyType *myVar;
@end
This can be accessed like such with dot- and smalltalk-based syntaxes:
Type *newVar1 = myClassInstance.myVar;
Type *newVar2 = [myClassInstance myVar];
myClassInstance.myVar = newVar1;
[myClassInstance setMyVar: newVar2];
And if I want to add some additional functionality to those getters/setters, I can do this:
@implementation MyClass
- (MyType *) myVar
{
// do more stuff
return self._myVar;
}
- (void) setMyVar: (MyType *) newVar
{
self._myVar = newVar;
// do more stuff
}
@end
(see also Custom setter for @property?)
However, the accepted answer to the above linked question says that Swift doesn't differentiate between properties and instance variable. So, say I have the following in Swift:
class MyClass {
var myVar: MyType
}
As far as I know, the only way to access myVar
is as such:
var newVar1 = myClassInstance.myVar;
myClassInstance.myVar = newVar1;
But I don't know how to customize these getters and setters. Is there a Swift equivalent of the Objective C @property
overrides?