mutableCopy in swift

时间:2014-09-22 18:25:24

标签: swift nsmutablearray

可以获得像(obj-c)

这样的可变副本
NSMutableArray *lists = [self.fetchedResultsController.fetchedObjects mutableCopy];

在swift?

我正在尝试用

重新安排一些核心数据实体
func tableView(tableView: UITableView, moveRowAtIndexPath sourceIndexPath: NSIndexPath, toIndexPath destinationIndexPath: NSIndexPath)

任何想法?

2 个答案:

答案 0 :(得分:3)

如果数据是值类型(结构,数组,字典,字符串等),只需将不可变变量分配给可变变量:

let immutable = [1, 2, 3]
var mutable = immutable
mutable[0] = 55 // mutable now is [55, 2, 3], whereas immutable is of course not modified

如上所述,这仅适用于值类型,因为赋值是通过值而不是通过引用来完成的。

答案 1 :(得分:0)

(self.fetchedResultsController.fetchedObjects as NSArray).mutableCopy() as! NSMutableArray