I am getting an error message when I try to access the private array (arr) inside the class (MyArray). What I am trying to achieve is to create a closure for reversing the array contents and this closure can be called from outside the class using instance variables. I am not able to understand this error. Can someone please help !!
public class MyArray {
private var arr: [Int] = []
internal static var instance: MyArray?
public var reverse = { () -> [Int] in
var returnArray : [Int] = []
var counter = arr.count - 1 // Error: Instance member 'arr' cannot be used on type 'MyArray'
for _ in (0...arr.count - 1) { // Error: Instance member 'arr' cannot be used on type 'MyArray'
returnArray.append(arr[counter])
counter -= 1
}
return returnArray
}
private init() {}
public static func getInstance() -> MyArray {
if self.instance == nil {
self.instance = MyArray()
}
return self.instance!
}
}