我希望通过引用将我的 Swift Arrayaccount.chats传递给 chatsViewController.chats(这样当我向 account.chats添加聊天时,chatsViewController.chats仍然指向 account.chats)。也就是说,当 account.chats的长度发生变化时,我不希望 Swift 将两个数组分开。
class Account {
var chats : [String]!
var chatsViewController : ChatsViewController!
func InitViewController() {
chatsViewController.getChats = { return self.chats }
}
}
class ChatsViewController {
var getChats: (() -> ([String]))!
func doSomethingWithChats() {
let chats = getChats()
// use it as needed
}
}
class MArray<T> : MutableCollection {
var array : Array<T> = Array()
var count: Int { return array.count } // getter (without, index func below will be called repeatedly to determine)
func add(_ value: T) {
array.append(value)
}
// MutableCollection requires:
subscript (index: Int) -> T {
get { return array[index] }
set(value) { array[index] = value }
}
var startIndex: Int {
return 0
}
var endIndex: Int {
return array.count
}
func index(after i: Int) -> Int {
return i+1
}
}