Reputation: 11
Is it possible to create a computed array property or does one need to create an overloaded function to accomplish this?
I.E. Could this be rewritten as a property
class myclass (
private var myInternalArray[pvtstrct]()
init(){
<loop building myInternalArray.insert(struct, at Index) .....>
}
func lfof(index: Int, value: String) {
myInternalArray(index) = PrivateCoversionFromString(value)
}
func lfof(index: Int) -> String {
return PrivateConversionToString(myInternalArray(index))
}
Upvotes: 0
Views: 2714
Reputation: 1159
It seems that the functionality that you are looking for is in the section on the official Swift reference on subscripts.
Your first function is a setter and the second one is a getter. You could use a subscript
to make it less awkward:
class MyClass {
private var _privateArray: [SomeType]
subscript(index: Int) -> String {
get {
return toString(_privateArray[index])
}
set(newValue) {
_privateArray[index] = fromString(newValue)
}
}
func fromString(value: String) -> SomeType {
// The code that turns a string into SomeType
}
func toString(value: SomeType) -> String {
// The code that turns SomeType into a string
}
}
This piece of code was adapted from The Swift Programming Language (Swift 2.2).
Upvotes: 2