根据Swift指南:
对于数组,只有在执行可能修改数组长度的操作时,才会进行复制。这包括附加、插入或删除项,或使用范围下标替换数组中的一系列项。
以
append
活动在后台,当您向数组追加时,Swift会创建一个
新
数组,从现有的
animals
数组到这个新数组中-加上新项-然后将这个新数组分配给
动物
变量这种花招就是为什么你只能得到
1
(
Setting
),因为事实上每个'
编辑
'实际上会创建一个新数组。
这与
NSMutableArray
因为行为更直观一点-编辑
是
创建到现有数组(没有后台复制到新数组),因此编辑后存在的数组与之前存在的数组相同,因此存储在
change
带钥匙的字典
NSKeyValueChangeKindKey
可以是以下之一
.Insertion
,
.Removal
等
然而,这并不是全部,因为您对KVO兼容的更改
NS可变阵列
取决于您使用的是Swift还是Objective-C。在Objective-C中,苹果强烈建议实现他们称之为可选的
mutable indexed accessors
这些只是具有标准签名的方法,以非常有效的方式进行符合KVO的更改。
// Mutable Accessors ///////////////////////////////////////////
// This class has a property called <children> of type NSMutableArray
// MUST have this (or other insert accessor)
-(void)insertObject:(NSNumber *)object inChildrenAtIndex:(NSUInteger)index {
[self.children insertObject:object atIndex:index];
}
// MUST have this (or other remove accessor)
-(void)removeObjectFromChildrenAtIndex:(NSUInteger)index {
[self.children removeObjectAtIndex:index];
}
// OPTIONAL - but a good idea.
-(void)replaceObjectInChildrenAtIndex:(NSUInteger)index withObject:(id)object {
[self.children replaceObjectAtIndex:index withObject:object];
}
Swift似乎不具备这些功能,因此进行KVO兼容更改的唯一方法是通过可变代理对象-如
NSKeyValueCoding
页下面是一个非常简单的例子:
@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
dynamic var children: NSMutableArray = NSMutableArray()
////////////////////////////////////
func applicationDidFinishLaunching(aNotification: NSNotification) {
addObserver(self,
forKeyPath: "children",
options: .New | .Old,
context: &Update)
// Get the KVO/KVC compatible array
var childrenProxy = mutableArrayValueForKey("children")
childrenProxy.addObject(NSNumber(integer: 20)) // .Insertion
childrenProxy.addObject(NSNumber(integer: 30)) // .Insertion
childrenProxy.removeObjectAtIndex(1) // .Removal
}
}